From: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
To: ojeda@kernel.org, rust-for-linux@vger.kernel.org
Cc: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>,
Kartik Prajapati <kartikprajapati987@gmail.com>
Subject: [PATCH v1] rust: kernel: types: Add `ARef::into_raw`
Date: Mon, 25 Mar 2024 12:04:26 +0100 [thread overview]
Message-ID: <20240325110451.42277-1-vincenzopalazzodev@gmail.com> (raw)
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
next reply other threads:[~2024-03-25 11:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-25 11:04 Vincenzo Palazzo [this message]
2024-03-25 14:10 ` [PATCH v1] rust: kernel: types: Add `ARef::into_raw` 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240325110451.42277-1-vincenzopalazzodev@gmail.com \
--to=vincenzopalazzodev@gmail.com \
--cc=kartikprajapati987@gmail.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).