mirror of
https://github.com/torvalds/linux.git
synced 2026-09-22 12:44:03 +02:00
rust: faux: Allow retrieving a bound Device
When writing up some rust code that used faux devices for unit testing, I noticed that we never actually added the Bound device context to faux::Registration's AsRef<device::Device> implementation. This being said: the Registration object itself is proof that a driver is bound to the device - so this should be safe. Signed-off-by: Lyude Paul <lyude@redhat.com> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://patch.msgid.link/20260612194436.585385-4-lyude@redhat.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
parent
616c229ab0
commit
fa8cc4e306
|
|
@ -25,7 +25,8 @@
|
|||
///
|
||||
/// # Invariants
|
||||
///
|
||||
/// `self.0` always holds a valid pointer to an initialized and registered [`struct faux_device`].
|
||||
/// - `self.0` always holds a valid pointer to an initialized and registered [`struct faux_device`].
|
||||
/// - This object is proof that the object described by this `Registration` is bound to a device.
|
||||
///
|
||||
/// [`struct faux_device`]: srctree/include/linux/device/faux.h
|
||||
pub struct Registration(NonNull<bindings::faux_device>);
|
||||
|
|
@ -59,10 +60,17 @@ fn as_raw(&self) -> *mut bindings::faux_device {
|
|||
}
|
||||
}
|
||||
|
||||
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`.
|
||||
impl AsRef<device::Device<device::Bound>> for Registration {
|
||||
fn as_ref(&self) -> &device::Device<device::Bound> {
|
||||
// SAFETY:
|
||||
// - The underlying `device` in `faux_device` is guaranteed by the C API to be a valid
|
||||
// initialized `device`.
|
||||
// - `faux_match()` always returns 1, and probe runs synchronously
|
||||
// (PROBE_FORCE_SYNCHRONOUS).
|
||||
// - `suppress_bind_attrs = true` on faux_driver prevents userspace-triggered unbind via
|
||||
// sysfs.
|
||||
// - `mem::forget(Registration)` is not a problem; if the `Registration` is leaked, the faux
|
||||
// device stays bound forever.
|
||||
unsafe { device::Device::from_raw(addr_of_mut!((*self.as_raw()).dev)) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user