rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] rust: kernel: types: Add `ARef::into_raw`
@ 2024-03-25 11:04 Vincenzo Palazzo
  2024-03-25 14:10 ` Miguel Ojeda
  0 siblings, 1 reply; 4+ messages in thread
From: Vincenzo Palazzo @ 2024-03-25 11:04 UTC (permalink / raw)
  To: ojeda, rust-for-linux; +Cc: Vincenzo Palazzo, Kartik Prajapati

Add the function `into_raw` to `ARef<T>`. This method
can be used to turn an `ARef` into a raw pointer.

Link: https://github.com/Rust-for-Linux/linux/issues/1044
Co-developed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
Signed-off-by: Kartik Prajapati <kartikprajapati987@gmail.com>
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
---
 rust/kernel/types.rs | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index aa77bad9bce4..b2a243bb810e 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -366,6 +366,38 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
             _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> {
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread
* [PATCH v1] rust: kernel: types: Add `ARef::into_raw`
@ 2024-03-24 23:12 Vincenzo Palazzo
  0 siblings, 0 replies; 4+ messages in thread
From: Vincenzo Palazzo @ 2024-03-24 23:12 UTC (permalink / raw)
  To: ojeda, rust-for-linux; +Cc: Vincenzo Palazzo, Kartik Prajapati

Add the function `into_raw` to `ARef<T>`. This method
can be used to turn an `ARef` into a raw pointer.

Link: https://github.com/Rust-for-Linux/linux/issues/1044
Co-developed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
Signed-off-by: Kartik Prajapati <kartikprajapati987@gmail.com>
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
---
 rust/kernel/types.rs | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index aa77bad9bce4..b2a243bb810e 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -366,6 +366,38 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
             _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> {
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-29 20:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-25 11:04 [PATCH v1] rust: kernel: types: Add `ARef::into_raw` Vincenzo Palazzo
2024-03-25 14:10 ` Miguel Ojeda
2024-03-29 20:11   ` Vincenzo Palazzo
  -- strict thread matches above, loose matches on Subject: below --
2024-03-24 23:12 Vincenzo Palazzo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).