Skip to content

Commit 10cef0c

Browse files
committed
rust: kernel: types: Add ARef::into_raw
Add the function `into_raw` to `ARef<T>`. This method can be used to turn an `ARef` into a raw pointer. Signed-off-by: Kartik Prajapati <[email protected]>
1 parent 711cbfc commit 10cef0c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

rust/kernel/types.rs

+32
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,38 @@ impl<T: AlwaysRefCounted> ARef<T> {
344344
_p: PhantomData,
345345
}
346346
}
347+
348+
/// Deconstructs a [`ARef`] object into a raw pointer.
349+
///
350+
/// It can be reconstructed once via [`ARef::from_raw`].
351+
///
352+
/// Note: This function does not decrement the reference count.
353+
///
354+
/// # Examples
355+
///
356+
/// ```
357+
/// use core::ptr::NonNull;
358+
/// use kernel::AlwaysRefCounted;
359+
///
360+
/// struct Empty {}
361+
///
362+
/// unsafe impl AlwaysRefCounted for Empty {
363+
/// fn inc_ref(&self) {}
364+
/// unsafe fn dec_ref(_obj: NonNull<Self>) {}
365+
/// }
366+
///
367+
/// let mut data = Empty {};
368+
/// let ptr = NonNull::<Empty>::new(&mut data as *mut _).unwrap();
369+
/// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
370+
/// let raw_ptr: *mut Empty = ARef::into_raw(data_ref);
371+
///
372+
/// assert_eq!(ptr.as_ptr(), raw_ptr);
373+
/// ```
374+
pub fn into_raw(obj: Self) -> *mut T {
375+
let ptr = obj.ptr.as_ptr();
376+
core::mem::forget(obj);
377+
ptr
378+
}
347379
}
348380

349381
impl<T: AlwaysRefCounted> Clone for ARef<T> {

0 commit comments

Comments
 (0)