Skip to content

Commit

Permalink
rust: kernel: types: Add ARef::into_raw
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Kartik1397 committed Jan 15, 2024
1 parent 711cbfc commit 9dad66b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions rust/kernel/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,36 @@ impl<T: AlwaysRefCounted> ARef<T> {
_p: PhantomData,
}
}

/// Deconstructs a [`ARef`] object into a raw pointer.
///
/// It can be reconstructed once via [`ARef::from_raw`].
///
/// # 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<Self>) {}
/// }
///
/// let mut data = Empty {};
/// let ptr = NonNull::<Empty>::new(&mut data as *mut _).unwrap();
/// let data_ref: ARef<Empty> = 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<T: AlwaysRefCounted> Clone for ARef<T> {
Expand Down

0 comments on commit 9dad66b

Please sign in to comment.