Skip to content

Commit 9adced6

Browse files
Darksonnfbq
authored andcommitted
rust: add ARef::into_raw
Add a method for `ARef` that is analogous to `Arc::into_raw`. It is the inverse operation of `ARef::from_raw`, and allows you to convert the `ARef` back into a raw pointer while retaining ownership of the refcount. This new function will be used by [1] for converting the type in an `ARef` using `ARef::from_raw(ARef::into_raw(me).cast())`. The author has also needed the same function for other use-cases in the past, but [1] is the first to go upstream. Link: https://lore.kernel.org/r/20240801-vma-v3-1-db6c1c0afda9@google.com [1] Signed-off-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20240801-aref-into-raw-v1-1-33401e2fbac8@google.com
1 parent c6c76e1 commit 9adced6

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

rust/kernel/types.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use alloc::boxed::Box;
77
use core::{
88
cell::UnsafeCell,
99
marker::{PhantomData, PhantomPinned},
10-
mem::MaybeUninit,
10+
mem::{ManuallyDrop, MaybeUninit},
1111
ops::{Deref, DerefMut},
1212
ptr::NonNull,
1313
};
@@ -411,6 +411,14 @@ impl<T: AlwaysRefCounted> ARef<T> {
411411
_p: PhantomData,
412412
}
413413
}
414+
415+
/// Consumes the `ARef`, returning a raw pointer.
416+
///
417+
/// This function does not change the refcount. After calling this function, the caller is
418+
/// responsible for the refcount previously managed by the `ARef`.
419+
pub fn into_raw(me: Self) -> NonNull<T> {
420+
ManuallyDrop::new(me).ptr
421+
}
414422
}
415423

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

0 commit comments

Comments
 (0)