Rename Device::as_ref() to from_raw()
Subsystems (12)
Alice Ryhl
maintainerv2 cover Rename Device::as_ref() to 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.
For more, see: https://lore.kernel.org/r/aCd8D5IA0RXZvtcv@pollux
---
Changes in v2:
- Split into two patches.
- Use a different lore link.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Alice Ryhl (2):
device: rust: rename Device::as_ref() to Device::from_raw()
drm: rust: rename as_ref() to from_raw() for drm constructors
rust/kernel/auxiliary.rs | 2 +- rust/kernel/cpu.rs | 2 +- rust/kernel/device.rs | 6 +++--- rust/kernel/drm/device.rs | 4 ++-- rust/kernel/drm/file.rs | 8 ++++---- rust/kernel/drm/gem/mod.rs | 16 ++++++++-------- rust/kernel/drm/ioctl.rs | 4 ++-- rust/kernel/faux.rs | 2 +- rust/kernel/miscdevice.rs | 2 +- rust/kernel/net/phy.rs | 2 +- rust/kernel/pci.rs | 2 +- rust/kernel/platform.rs | 2 +- 12 files changed, 26 insertions(+), 26 deletions(-)
---
base-commit: 86731a2a651e58953fc949573895f2fa6d456841
change-id: 20250709-device-as-ref-350db5317dd1
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Alice Ryhl
maintainerPATCH v2 1/2 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.
--- rust/kernel/auxiliary.rs | 2 +- rust/kernel/cpu.rs | 2 +- rust/kernel/device.rs | 6 +++--- rust/kernel/drm/device.rs | 2 +- rust/kernel/faux.rs | 2 +- rust/kernel/miscdevice.rs | 2 +- rust/kernel/net/phy.rs | 2 +- rust/kernel/pci.rs | 2 +- rust/kernel/platform.rs | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-)
rust/kernel/auxiliary.rs +1 -1
@@ -276,7 +276,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) } } }
rust/kernel/cpu.rs +1 -1
@@ -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) }) }
rust/kernel/device.rs +3 -3
@@ -57,7 +57,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 () } }
@@ -82,7 +82,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) }) } }
@@ -94,7 +94,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 ) -> &'aSelf { +pub unsafe fn from_raw <'a>(ptr: *mut bindings::device ) -> &'aSelf {// SAFETY: Guaranteed by the safety requirements of the function. unsafe { &*ptr.cast () } }
rust/kernel/drm/device.rs +1 -1
@@ -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) } } }
rust/kernel/faux.rs +1 -1
@@ -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)) } } }
rust/kernel/miscdevice.rs +1 -1
@@ -92,7 +92,7 @@ pub fn device(&self) -> &Device {// function tells the borrow-checker that the `&Device` reference must not outlive the // `&MiscDeviceRegistration<T>` used to obtain it, so the last use of the reference must be // before the underlying `struct miscdevice` is destroyed. -unsafe {Device ::as_ref ((*self.as_raw ()).this_device) } +unsafe {Device ::from_raw ((*self.as_raw ()).this_device) } } }
rust/kernel/net/phy.rs +1 -1
@@ -285,7 +285,7 @@ impl AsRef<kernel::device::Device> for Device {fn as_ref (&self) -> &kernel::device::Device {let phydev = self.0 .get ();// SAFETY: The struct invariant ensures that `mdio.dev` is valid. -unsafe { kernel::device::Device ::as_ref (addr_of_mut! ((*phydev).mdio.dev)) } +unsafe { kernel::device::Device ::from_raw (addr_of_mut! ((*phydev).mdio.dev)) } } }
rust/kernel/pci.rs +1 -1
@@ -454,7 +454,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) } } }
rust/kernel/platform.rs +1 -1
@@ -215,7 +215,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) } } }
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Re: [PATCH v2 1/2] device: rust: rename Device::as_ref() to Device::from_raw()
Quoted 6 lines · Alice Ryhl
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.
Quoted 6 lines
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.
---
Cheers,
Benno
Quoted 11 lines
--- rust/kernel/auxiliary.rs | 2 +- rust/kernel/cpu.rs | 2 +- rust/kernel/device.rs | 6 +++--- rust/kernel/drm/device.rs | 2 +- rust/kernel/faux.rs | 2 +- rust/kernel/miscdevice.rs | 2 +- rust/kernel/net/phy.rs | 2 +- rust/kernel/pci.rs | 2 +- rust/kernel/platform.rs | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-)
Danilo Krummrich
maintainerRe: [PATCH v2 1/2] device: rust: rename Device::as_ref() to Device::from_raw()
In reply to Alice RyhlQuoted 6 lines · Alice Ryhl
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.
Quoted 6 lines
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.
FUJITA, Thomas, Peter: Unless there are any concerns, I'll pick this one up soon.
Re: [PATCH v2 1/2] device: rust: rename Device::as_ref() to Device::from_raw()
In reply to Alice RyhlQuoted 6 lines · Alice Ryhl
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.
Quoted 6 lines
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.
Danilo Krummrich
maintainerRe: [PATCH v2 1/2] device: rust: rename Device::as_ref() to Device::from_raw()
In reply to Alice RyhlQuoted 6 lines · Alice Ryhl
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.
Quoted 6 lines
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.
Applied to driver-core-testing, thanks!
Alice Ryhl
maintainerPATCH v2 2/2 drm: rust: rename as_ref() to from_raw() for drm constructors
In reply to Alice RyhlThe 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.
--- rust/kernel/drm/device.rs | 2 +- rust/kernel/drm/file.rs | 8 ++++---- rust/kernel/drm/gem/mod.rs | 16 ++++++++-------- rust/kernel/drm/ioctl.rs | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-)
rust/kernel/drm/device.rs +1 -1
@@ -154,7 +154,7 @@ unsafe fn from_drm_device(ptr: *const bindings::drm_device) -> *mut Self {/// Additionally, callers must ensure that the `struct device`, `ptr` is pointing to, is /// embedded in `Self`. #[doc(hidden)] -pub unsafe fn as_ref <'a>(ptr: *const bindings::drm_device ) -> &'aSelf { +pub unsafe fn from_raw <'a>(ptr: *const bindings::drm_device ) -> &'aSelf {// SAFETY: By the safety requirements of this function `ptr` is a valid pointer to a // `struct drm_device` embedded in `Self`. let ptr =unsafe {Self ::from_drm_device (ptr) };
rust/kernel/drm/file.rs +4 -4
@@ -32,7 +32,7 @@ impl<T: DriverFile> File<T> {/// # Safety /// /// `raw_file` must be a valid pointer to an open `struct drm_file`, opened through `T::open`. -pub unsafe fn as_ref <'a>(ptr: *mut bindings::drm_file ) -> &'aFile <T > { +pub unsafe fn from_raw <'a>(ptr: *mut bindings::drm_file ) -> &'aFile <T > {// SAFETY: `raw_file` is valid by the safety requirements of this function. unsafe { &*ptr.cast () } }
@@ -61,10 +61,10 @@ pub(crate) extern "C" fn open_callback(// SAFETY: A callback from `struct drm_driver::open` guarantees that // - `raw_dev` is valid pointer to a `struct drm_device`, // - the corresponding `struct drm_device` has been registered. -let drm =unsafe { drm::Device ::as_ref (raw_dev) }; +let drm =unsafe { drm::Device ::from_raw (raw_dev) };// SAFETY: `raw_file` is a valid pointer to a `struct drm_file`. -let file =unsafe {File ::<T >::as_ref (raw_file) }; +let file =unsafe {File ::<T >::from_raw (raw_file) };let inner =match T ::open (drm) {Err (e) => {
@@ -89,7 +89,7 @@ pub(crate) extern "C" fn postclose_callback( raw_file: *mut bindings::drm_file, ) {// SAFETY: This reference won't escape this function -let file =unsafe {File ::<T >::as_ref (raw_file) }; +let file =unsafe {File ::<T >::from_raw (raw_file) };// SAFETY: `file.driver_priv` has been created in `open_callback` through `KBox::into_raw`. let _ =unsafe {KBox ::from_raw (file.driver_priv ()) };
rust/kernel/drm/gem/mod.rs +8 -8
@@ -51,7 +51,7 @@ pub trait IntoGEMObject: Sized + super::private::Sealed + AlwaysRefCounted {/// - `self_ptr` must be a valid pointer to `Self`. /// - The caller promises that holding the immutable reference returned by this function does /// not violate rust's data aliasing rules and remains valid throughout the lifetime of `'a`. -unsafe fn as_ref <'a>(self_ptr: *mut bindings::drm_gem_object ) -> &'aSelf ; +unsafe fn from_raw <'a>(self_ptr: *mut bindings::drm_gem_object ) -> &'aSelf ; }// SAFETY: All gem objects are refcounted.
@@ -86,12 +86,12 @@ extern "C" fn open_callback<T: BaseDriverObject<U>, U: BaseObject>( ) -> core::ffi::c_int {// SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`. let file =unsafe { - drm::File ::<<<U as IntoGEMObject >::Driver as drm::Driver >::File >::as_ref (raw_file) + drm::File ::<<<U as IntoGEMObject >::Driver as drm::Driver >::File >::from_raw (raw_file) };// SAFETY: `open_callback` is specified in the AllocOps structure for `Object<T>`, ensuring that // `raw_obj` is indeed contained within a `Object<T>`. let obj =unsafe { - <<<U as IntoGEMObject >::Driver as drm::Driver >::Object as IntoGEMObject >::as_ref (raw_obj) + <<<U as IntoGEMObject >::Driver as drm::Driver >::Object as IntoGEMObject >::from_raw (raw_obj) };match T ::open (obj, file) {
@@ -106,12 +106,12 @@ extern "C" fn close_callback<T: BaseDriverObject<U>, U: BaseObject>( ) {// SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`. let file =unsafe { - drm::File ::<<<U as IntoGEMObject >::Driver as drm::Driver >::File >::as_ref (raw_file) + drm::File ::<<<U as IntoGEMObject >::Driver as drm::Driver >::File >::from_raw (raw_file) };// SAFETY: `close_callback` is specified in the AllocOps structure for `Object<T>`, ensuring // that `raw_obj` is indeed contained within a `Object<T>`. let obj =unsafe { - <<<U as IntoGEMObject >::Driver as drm::Driver >::Object as IntoGEMObject >::as_ref (raw_obj) + <<<U as IntoGEMObject >::Driver as drm::Driver >::Object as IntoGEMObject >::from_raw (raw_obj) };T ::close (obj, file);
@@ -124,7 +124,7 @@ fn as_raw(&self) -> *mut bindings::drm_gem_object { self.obj.get () } -unsafe fn as_ref <'a>(self_ptr: *mut bindings::drm_gem_object ) -> &'aSelf { +unsafe fn from_raw <'a>(self_ptr: *mut bindings::drm_gem_object ) -> &'aSelf {let self_ptr: *mut Opaque <bindings::drm_gem_object > = self_ptr.cast ();// SAFETY: `obj` is guaranteed to be in an `Object<T>` via the safety contract of this
@@ -170,9 +170,9 @@ fn lookup_handle(// - A `drm::Driver` can only have a single `File` implementation. // - `file` uses the same `drm::Driver` as `Self`. // - Therefore, we're guaranteed that `ptr` must be a gem object embedded within `Self`. -// - And we check if the pointer is null befoe calling as_ref(), ensuring that `ptr` is a +// - And we check if the pointer is null befoe calling from_raw(), ensuring that `ptr` is a // valid pointer to an initialized `Self`. -let obj =unsafe {Self ::as_ref (ptr) }; +let obj =unsafe {Self ::from_raw (ptr) };// SAFETY: // - We take ownership of the reference of `drm_gem_object_lookup()`.
rust/kernel/drm/ioctl.rs +2 -2
@@ -134,7 +134,7 @@ macro_rules! declare_drm_ioctls {// FIXME: Currently there is nothing enforcing that the types of the // dev/file match the current driver these ioctls are being declared // for, and it's not clear how to enforce this within the type system. -let dev = $crate::drm::device::Device ::as_ref (raw_dev); +let dev = $crate::drm::device::Device ::from_raw (raw_dev);// SAFETY: The ioctl argument has size `_IOC_SIZE(cmd)`, which we // asserted above matches the size of this type, and all bit patterns of // UAPI structs must be valid.
@@ -142,7 +142,7 @@ macro_rules! declare_drm_ioctls { &*(raw_data as *const $crate::types::Opaque<$crate::uapi::$struct>) };// SAFETY: This is just the DRM file structure -let file =unsafe { $crate::drm::File ::as_ref (raw_file) }; +let file =unsafe { $crate::drm::File ::from_raw (raw_file) };match $func(dev, data, file) {Err (e) => e.to_errno (),
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Re: [PATCH v2 2/2] drm: rust: rename as_ref() to from_raw() for drm constructors
Quoted 6 lines · Alice Ryhl
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.
Quoted 6 lines
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.
---
Cheers,
Benno
Quoted 6 lines
--- rust/kernel/drm/device.rs | 2 +- rust/kernel/drm/file.rs | 8 ++++---- rust/kernel/drm/gem/mod.rs | 16 ++++++++-------- rust/kernel/drm/ioctl.rs | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-)
Re: [PATCH v2 2/2] drm: rust: rename as_ref() to from_raw() for drm constructors
In reply to Alice RyhlQuoted 6 lines · Alice Ryhl
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.
Quoted 6 lines
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.
Applied to drm-misc-next, thanks!