Skip to content

Commit 2f5606a

Browse files
DarksonnDanilo Krummrich
authored andcommitted
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: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20250711-device-as-ref-v2-1-1b16ab6402d7@google.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent cbf2186 commit 2f5606a

9 files changed

Lines changed: 11 additions & 11 deletions

File tree

rust/kernel/auxiliary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
270270
let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };
271271

272272
// SAFETY: `dev` points to a valid `struct device`.
273-
unsafe { device::Device::as_ref(dev) }
273+
unsafe { device::Device::from_raw(dev) }
274274
}
275275
}
276276

rust/kernel/cpu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,5 @@ pub unsafe fn from_cpu(cpu: CpuId) -> Result<&'static Device> {
147147

148148
// SAFETY: The pointer returned by `get_cpu_device()`, if not `NULL`, is a valid pointer to
149149
// a `struct device` and is never freed by the C code.
150-
Ok(unsafe { Device::as_ref(ptr) })
150+
Ok(unsafe { Device::from_raw(ptr) })
151151
}

rust/kernel/device.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl Device {
5858
/// While not officially documented, this should be the case for any `struct device`.
5959
pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> {
6060
// SAFETY: By the safety requirements ptr is valid
61-
unsafe { Self::as_ref(ptr) }.into()
61+
unsafe { Self::from_raw(ptr) }.into()
6262
}
6363

6464
/// Convert a [`&Device`](Device) into a [`&Device<Bound>`](Device<Bound>).
@@ -149,7 +149,7 @@ impl<Ctx: DeviceContext> Device<Ctx> {
149149
// - Since `parent` is not NULL, it must be a valid pointer to a `struct device`.
150150
// - `parent` is valid for the lifetime of `self`, since a `struct device` holds a
151151
// reference count of its parent.
152-
Some(unsafe { Self::as_ref(parent) })
152+
Some(unsafe { Self::from_raw(parent) })
153153
}
154154
}
155155

@@ -161,7 +161,7 @@ impl<Ctx: DeviceContext> Device<Ctx> {
161161
/// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to
162162
/// can't drop to zero, for the duration of this function call and the entire duration when the
163163
/// returned reference exists.
164-
pub unsafe fn as_ref<'a>(ptr: *mut bindings::device) -> &'a Self {
164+
pub unsafe fn from_raw<'a>(ptr: *mut bindings::device) -> &'a Self {
165165
// SAFETY: Guaranteed by the safety requirements of the function.
166166
unsafe { &*ptr.cast() }
167167
}

rust/kernel/drm/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl<T: drm::Driver> AsRef<device::Device> for Device<T> {
190190
fn as_ref(&self) -> &device::Device {
191191
// SAFETY: `bindings::drm_device::dev` is valid as long as the DRM device itself is valid,
192192
// which is guaranteed by the type invariant.
193-
unsafe { device::Device::as_ref((*self.as_raw()).dev) }
193+
unsafe { device::Device::from_raw((*self.as_raw()).dev) }
194194
}
195195
}
196196

rust/kernel/faux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl AsRef<device::Device> for Registration {
5454
fn as_ref(&self) -> &device::Device {
5555
// SAFETY: The underlying `device` in `faux_device` is guaranteed by the C API to be
5656
// a valid initialized `device`.
57-
unsafe { device::Device::as_ref(addr_of_mut!((*self.as_raw()).dev)) }
57+
unsafe { device::Device::from_raw(addr_of_mut!((*self.as_raw()).dev)) }
5858
}
5959
}
6060

rust/kernel/miscdevice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<T: MiscDevice> MiscDeviceRegistration<T> {
9898
// function tells the borrow-checker that the `&Device` reference must not outlive the
9999
// `&MiscDeviceRegistration<T>` used to obtain it, so the last use of the reference must be
100100
// before the underlying `struct miscdevice` is destroyed.
101-
unsafe { Device::as_ref((*self.as_raw()).this_device) }
101+
unsafe { Device::from_raw((*self.as_raw()).this_device) }
102102
}
103103
}
104104

rust/kernel/net/phy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl AsRef<kernel::device::Device> for Device {
285285
fn as_ref(&self) -> &kernel::device::Device {
286286
let phydev = self.0.get();
287287
// SAFETY: The struct invariant ensures that `mdio.dev` is valid.
288-
unsafe { kernel::device::Device::as_ref(addr_of_mut!((*phydev).mdio.dev)) }
288+
unsafe { kernel::device::Device::from_raw(addr_of_mut!((*phydev).mdio.dev)) }
289289
}
290290
}
291291

rust/kernel/pci.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
468468
let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };
469469

470470
// SAFETY: `dev` points to a valid `struct device`.
471-
unsafe { device::Device::as_ref(dev) }
471+
unsafe { device::Device::from_raw(dev) }
472472
}
473473
}
474474

rust/kernel/platform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
251251
let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };
252252

253253
// SAFETY: `dev` points to a valid `struct device`.
254-
unsafe { device::Device::as_ref(dev) }
254+
unsafe { device::Device::from_raw(dev) }
255255
}
256256
}
257257

0 commit comments

Comments
 (0)