diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 2e7c9008621f5d..cbab2493445fcd 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -366,6 +366,38 @@ impl ARef { _p: PhantomData, } } + + /// Deconstructs a [`ARef`] object into a raw pointer. + /// + /// It can be reconstructed once via [`ARef::from_raw`]. + /// + /// Note: This function does not decrement the reference count. + /// + /// # Examples + /// + /// ``` + /// use core::ptr::NonNull; + /// use kernel::AlwaysRefCounted; + /// + /// struct Empty {} + /// + /// unsafe impl AlwaysRefCounted for Empty { + /// fn inc_ref(&self) {} + /// unsafe fn dec_ref(_obj: NonNull) {} + /// } + /// + /// let mut data = Empty {}; + /// let ptr = NonNull::::new(&mut data as *mut _).unwrap(); + /// let data_ref: ARef = unsafe { ARef::from_raw(ptr) }; + /// let raw_ptr: *mut Empty = ARef::into_raw(data_ref); + /// + /// assert_eq!(ptr.as_ptr(), raw_ptr); + /// ``` + pub fn into_raw(obj: Self) -> *mut T { + let ptr = obj.ptr.as_ptr(); + core::mem::forget(obj); + ptr + } } impl Clone for ARef {