Skip to content

Commit 9dad66b

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 9dad66b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

rust/kernel/types.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,36 @@ 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+
/// # Examples
353+
///
354+
/// ```
355+
/// use core::ptr::NonNull;
356+
/// use kernel::AlwaysRefCounted;
357+
///
358+
/// struct Empty {}
359+
///
360+
/// unsafe impl AlwaysRefCounted for Empty {
361+
/// fn inc_ref(&self) {}
362+
/// unsafe fn dec_ref(_obj: NonNull<Self>) {}
363+
/// }
364+
///
365+
/// let mut data = Empty {};
366+
/// let ptr = NonNull::<Empty>::new(&mut data as *mut _).unwrap();
367+
/// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
368+
/// let raw_ptr: *mut Empty = ARef::into_raw(data_ref);
369+
///
370+
/// assert_eq!(ptr.as_ptr(), raw_ptr);
371+
/// ```
372+
pub fn into_raw(obj: Self) -> *mut T {
373+
let ptr = obj.ptr.as_ptr();
374+
core::mem::forget(obj);
375+
ptr
376+
}
347377
}
348378

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

0 commit comments

Comments
 (0)