mirror of
git://git.yoctoproject.org/linux-yocto.git
synced 2026-01-27 12:47:24 +01:00
rust: device: implement Device::as_bound()
Provide an unsafe functions for abstractions to convert a regular &Device to a &Device<Bound>. This is useful for registrations that provide certain guarantees for the scope of their callbacks, such as IRQs or certain class device registrations (e.g. PWM, miscdevice). Reviewed-by: Benno Lossin <lossin@kernel.org> Link: https://lore.kernel.org/r/20250713182737.64448-2-dakr@kernel.org [ Remove unnecessary cast(). - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
parent
85aa5b16fe
commit
47e6715bb7
|
|
@ -60,6 +60,25 @@ impl Device {
|
|||
// SAFETY: By the safety requirements ptr is valid
|
||||
unsafe { Self::as_ref(ptr) }.into()
|
||||
}
|
||||
|
||||
/// Convert a [`&Device`](Device) into a [`&Device<Bound>`](Device<Bound>).
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The caller is responsible to ensure that the returned [`&Device<Bound>`](Device<Bound>)
|
||||
/// only lives as long as it can be guaranteed that the [`Device`] is actually bound.
|
||||
pub unsafe fn as_bound(&self) -> &Device<Bound> {
|
||||
let ptr = core::ptr::from_ref(self);
|
||||
|
||||
// CAST: By the safety requirements the caller is responsible to guarantee that the
|
||||
// returned reference only lives as long as the device is actually bound.
|
||||
let ptr = ptr.cast();
|
||||
|
||||
// SAFETY:
|
||||
// - `ptr` comes from `from_ref(self)` above, hence it's guaranteed to be valid.
|
||||
// - Any valid `Device` pointer is also a valid pointer for `Device<Bound>`.
|
||||
unsafe { &*ptr }
|
||||
}
|
||||
}
|
||||
|
||||
impl Device<CoreInternal> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user