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.

Link: Rust-for-Linux#1044
Co-developed-by: Vincenzo Palazzo <[email protected]>
Signed-off-by: Kartik Prajapati <[email protected]>
  • Loading branch information
Kartik1397 authored and vincenzopalazzo committed Jun 8, 2024
1 parent bf97be0 commit bb0a3bf
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions rust/kernel/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,38 @@ impl<T: AlwaysRefCounted> ARef<T> {
_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<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 bb0a3bf

Please sign in to comment.