Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: types: 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())`. Alice has
also needed the same function for other use-cases in the past, but [1]
is the first to go upstream.

This was implemented independently by Kartik and Alice. The two versions
were merged by Alice, so all mistakes are Alice's.

Link: https://lore.kernel.org/r/20240801-vma-v3-1-db6c1c0afda9@google.com [1]
Link: https://github.com/Rust-for-Linux/linux/issues/1044
Signed-off-by: Kartik Prajapati <kartikprajapati987@gmail.com>
Co-developed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
[ Reworded to correct the author reference and changed tag to Link
since it is not a bug. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Kartik Prajapati and committed by
Miguel Ojeda
96fff2dc c7305116

+30 -1
+30 -1
rust/kernel/types.rs
··· 7 7 use core::{ 8 8 cell::UnsafeCell, 9 9 marker::{PhantomData, PhantomPinned}, 10 - mem::MaybeUninit, 10 + mem::{ManuallyDrop, MaybeUninit}, 11 11 ops::{Deref, DerefMut}, 12 12 pin::Pin, 13 13 ptr::NonNull, ··· 395 395 ptr, 396 396 _p: PhantomData, 397 397 } 398 + } 399 + 400 + /// Consumes the `ARef`, returning a raw pointer. 401 + /// 402 + /// This function does not change the refcount. After calling this function, the caller is 403 + /// responsible for the refcount previously managed by the `ARef`. 404 + /// 405 + /// # Examples 406 + /// 407 + /// ``` 408 + /// use core::ptr::NonNull; 409 + /// use kernel::types::{ARef, AlwaysRefCounted}; 410 + /// 411 + /// struct Empty {} 412 + /// 413 + /// unsafe impl AlwaysRefCounted for Empty { 414 + /// fn inc_ref(&self) {} 415 + /// unsafe fn dec_ref(_obj: NonNull<Self>) {} 416 + /// } 417 + /// 418 + /// let mut data = Empty {}; 419 + /// let ptr = NonNull::<Empty>::new(&mut data as *mut _).unwrap(); 420 + /// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) }; 421 + /// let raw_ptr: NonNull<Empty> = ARef::into_raw(data_ref); 422 + /// 423 + /// assert_eq!(ptr, raw_ptr); 424 + /// ``` 425 + pub fn into_raw(me: Self) -> NonNull<T> { 426 + ManuallyDrop::new(me).ptr 398 427 } 399 428 } 400 429