rust/kernel/types.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
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>
---
This change was previously included in
https://lore.kernel.org/all/20240801-vma-v3-1-db6c1c0afda9@google.com/
but is being split out in its own commit at Danilo's suggestion.
---
rust/kernel/types.rs | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index bd189d646adb..83f20f589ef6 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -7,7 +7,7 @@
use core::{
cell::UnsafeCell,
marker::{PhantomData, PhantomPinned},
- mem::MaybeUninit,
+ mem::{ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut},
ptr::NonNull,
};
@@ -366,6 +366,14 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
_p: PhantomData,
}
}
+
+ /// Consumes the `ARef`, returning a raw pointer.
+ ///
+ /// This function does not change the refcount. After calling this function, the caller is
+ /// responsible for the refcount previously managed by the `ARef`.
+ pub fn into_raw(me: Self) -> NonNull<T> {
+ ManuallyDrop::new(me).ptr
+ }
}
impl<T: AlwaysRefCounted> Clone for ARef<T> {
---
base-commit: 8400291e289ee6b2bf9779ff1c83a291501f017b
change-id: 20240801-aref-into-raw-e0518131442f
Best regards,
--
Alice Ryhl <aliceryhl@google.com>
On 8/1/24 4:16 PM, Alice Ryhl wrote: > 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>
On Thu, Aug 1, 2024 at 4:17 PM Alice Ryhl <aliceryhl@google.com> wrote: > > 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> Actually I just realized that there's already another use on the list in https://lore.kernel.org/all/20240725-alice-file-v8-0-55a2e80deaa8@google.com/
On Thu, Aug 01, 2024 at 04:45:02PM +0200, Alice Ryhl wrote: > On Thu, Aug 1, 2024 at 4:17 PM Alice Ryhl <aliceryhl@google.com> wrote: > > > > 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> > > Actually I just realized that there's already another use on the list in > https://lore.kernel.org/all/20240725-alice-file-v8-0-55a2e80deaa8@google.com/ Yes, I was about to point this out ;-) I picked this up in rust-dev and rebased file patches onto it for testing purpose. In the meanwhile, Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Regards, Boqun
© 2016 - 2026 Red Hat, Inc.