From 9d18ce841f34988eb7e31403406d9191aac09045 Mon Sep 17 00:00:00 2001 From: Kartik Prajapati Date: Sat, 13 Jan 2024 17:14:45 +0530 Subject: [PATCH] rust: kernel: types: Add `ARef::into_raw` Add the function `into_raw` to `ARef`. This method can be used to turn an `ARef` into a raw pointer. Link: https://github.com/Rust-for-Linux/linux/issues/1044 Co-developed-by: Vincenzo Palazzo Signed-off-by: Kartik Prajapati --- rust/kernel/types.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index aa77bad9bce486..b2a243bb810e39 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 {