Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ARef::into_raw #1056

Open
wants to merge 1 commit into
base: rust-next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions rust/kernel/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,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