Commit 2f5606af authored by Alice Ryhl's avatar Alice Ryhl Committed by Danilo Krummrich
Browse files

device: rust: rename Device::as_ref() to Device::from_raw()

The prefix as_* should not be used for a constructor. Constructors
usually use the prefix from_* instead.

Some prior art in the stdlib: Box::from_raw, CString::from_raw,
Rc::from_raw, Arc::from_raw, Waker::from_raw, File::from_raw_fd.

There is also prior art in the kernel crate: cpufreq::Policy::from_raw,
fs::File::from_raw_file, Kuid::from_raw, ARef::from_raw,
SeqFile::from_raw, VmaNew::from_raw, Io::from_raw.

Link: https://lore.kernel.org/r/aCd8D5IA0RXZvtcv@pollux


Signed-off-by: default avatarAlice Ryhl <aliceryhl@google.com>
Reviewed-by: default avatarBenno Lossin <lossin@kernel.org>
Reviewed-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://lore.kernel.org/r/20250711-device-as-ref-v2-1-1b16ab6402d7@google.com


Signed-off-by: default avatarDanilo Krummrich <dakr@kernel.org>
parent cbf21862
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -270,7 +270,7 @@ fn as_ref(&self) -> &device::Device<Ctx> {
        let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };

        // SAFETY: `dev` points to a valid `struct device`.
        unsafe { device::Device::as_ref(dev) }
        unsafe { device::Device::from_raw(dev) }
    }
}

+1 −1
Original line number Diff line number Diff line
@@ -147,5 +147,5 @@ pub unsafe fn from_cpu(cpu: CpuId) -> Result<&'static Device> {

    // SAFETY: The pointer returned by `get_cpu_device()`, if not `NULL`, is a valid pointer to
    // a `struct device` and is never freed by the C code.
    Ok(unsafe { Device::as_ref(ptr) })
    Ok(unsafe { Device::from_raw(ptr) })
}
+3 −3
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ impl Device {
    /// While not officially documented, this should be the case for any `struct device`.
    pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> {
        // SAFETY: By the safety requirements ptr is valid
        unsafe { Self::as_ref(ptr) }.into()
        unsafe { Self::from_raw(ptr) }.into()
    }

    /// Convert a [`&Device`](Device) into a [`&Device<Bound>`](Device<Bound>).
@@ -149,7 +149,7 @@ pub(crate) fn parent(&self) -> Option<&Self> {
            // - Since `parent` is not NULL, it must be a valid pointer to a `struct device`.
            // - `parent` is valid for the lifetime of `self`, since a `struct device` holds a
            //   reference count of its parent.
            Some(unsafe { Self::as_ref(parent) })
            Some(unsafe { Self::from_raw(parent) })
        }
    }

@@ -161,7 +161,7 @@ pub(crate) fn parent(&self) -> Option<&Self> {
    /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to
    /// can't drop to zero, for the duration of this function call and the entire duration when the
    /// returned reference exists.
    pub unsafe fn as_ref<'a>(ptr: *mut bindings::device) -> &'a Self {
    pub unsafe fn from_raw<'a>(ptr: *mut bindings::device) -> &'a Self {
        // SAFETY: Guaranteed by the safety requirements of the function.
        unsafe { &*ptr.cast() }
    }
+1 −1
Original line number Diff line number Diff line
@@ -190,7 +190,7 @@ impl<T: drm::Driver> AsRef<device::Device> for Device<T> {
    fn as_ref(&self) -> &device::Device {
        // SAFETY: `bindings::drm_device::dev` is valid as long as the DRM device itself is valid,
        // which is guaranteed by the type invariant.
        unsafe { device::Device::as_ref((*self.as_raw()).dev) }
        unsafe { device::Device::from_raw((*self.as_raw()).dev) }
    }
}

+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ impl AsRef<device::Device> for Registration {
    fn as_ref(&self) -> &device::Device {
        // SAFETY: The underlying `device` in `faux_device` is guaranteed by the C API to be
        // a valid initialized `device`.
        unsafe { device::Device::as_ref(addr_of_mut!((*self.as_raw()).dev)) }
        unsafe { device::Device::from_raw(addr_of_mut!((*self.as_raw()).dev)) }
    }
}

Loading