* [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
* [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
* Re: [PATCH v1] rust: kernel: types: Add `ARef::into_raw`
2024-03-25 11:04 Vincenzo Palazzo
@ 2024-03-25 14:10 ` Miguel Ojeda
2024-03-29 20:11 ` Vincenzo Palazzo
0 siblings, 1 reply; 4+ messages in thread
From: Miguel Ojeda @ 2024-03-25 14:10 UTC (permalink / raw)
To: Vincenzo Palazzo; +Cc: ojeda, rust-for-linux, Kartik Prajapati
Hi Vincenzo,
Thanks for sending this!
(This is a resend -- apparently vger has had some delays for the last
~14 hours or so.)
On Mon, Mar 25, 2024 at 12:05 PM Vincenzo Palazzo
<vincenzopalazzodev@gmail.com> wrote:
>
> 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>
Given what you wrote here, I think you intended to give the primary
authorship to Kartik, right? If that is the case, that would require a
different ordering of these lines (like the last example at
https://docs.kernel.org/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by):
Signed-off-by: Kartik Prajapati <kartikprajapati987@gmail.com>
Co-developed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
In addition, the commit should be properly attributed to Kartik in
Git, which means you will automatically get the `From: Kartik ...`
line too that you see in the example of the documentation I linked
above.
However, from what I can tell, you didn't modify the commit from
Kartik at all. If that is correct, then the `Co-developed-by` should
not be there (https://docs.kernel.org/process/submitting-patches.html#developer-s-certificate-of-origin-1-1
has the details).
Finally, for v2, could you please To/Cc the rest of the
maintainers/reviewers? You can use `scripts/get_maintainer.pl` for
that, or the couple commands at
https://rust-for-linux.com/contributing#submitting-patches.
Thanks!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1] rust: kernel: types: Add `ARef::into_raw`
2024-03-25 14:10 ` Miguel Ojeda
@ 2024-03-29 20:11 ` Vincenzo Palazzo
0 siblings, 0 replies; 4+ messages in thread
From: Vincenzo Palazzo @ 2024-03-29 20:11 UTC (permalink / raw)
To: Miguel Ojeda; +Cc: ojeda, rust-for-linux, Kartik Prajapati
> Hi Vincenzo,
>
> Thanks for sending this!
>
> (This is a resend -- apparently vger has had some delays for the last
> ~14 hours or so.)
Yeah sorry about the double posting on the ML, but looks like that now
all is fine.
Thanks
Vincent.
>
> On Mon, Mar 25, 2024 at 12:05 PM Vincenzo Palazzo
> <vincenzopalazzodev@gmail.com> wrote:
> >
> > 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>
>
> Given what you wrote here, I think you intended to give the primary
> authorship to Kartik, right? If that is the case, that would require a
> different ordering of these lines (like the last example at
> https://docs.kernel.org/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by):
>
> Signed-off-by: Kartik Prajapati <kartikprajapati987@gmail.com>
> Co-developed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
> Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
>
> In addition, the commit should be properly attributed to Kartik in
> Git, which means you will automatically get the `From: Kartik ...`
> line too that you see in the example of the documentation I linked
> above.
>
> However, from what I can tell, you didn't modify the commit from
> Kartik at all. If that is correct, then the `Co-developed-by` should
> not be there (https://docs.kernel.org/process/submitting-patches.html#developer-s-certificate-of-origin-1-1
> has the details).
>
> Finally, for v2, could you please To/Cc the rest of the
> maintainers/reviewers? You can use `scripts/get_maintainer.pl` for
> that, or the couple commands at
> https://rust-for-linux.com/contributing#submitting-patches.
>
> Thanks!
>
> Cheers,
> Miguel
^ permalink raw reply [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-24 23:12 [PATCH v1] rust: kernel: types: Add `ARef::into_raw` Vincenzo Palazzo
-- strict thread matches above, loose matches on Subject: below --
2024-03-25 11:04 Vincenzo Palazzo
2024-03-25 14:10 ` Miguel Ojeda
2024-03-29 20:11 ` 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).