rust: sync: add `UniqueArc::as_ptr`
Andreas Hindborg
reviewerPATCH 1/1 rust: sync: add `UniqueArc::as_ptr`
Add a method to UniqueArc for getting a raw pointer. The implementation defers to the Arc implementation of the same method.
--- rust/kernel/sync/arc.rs | 5 +++++ 1 file changed, 5 insertions(+)
rust/kernel/sync/arc.rs +5 -0
@@ -770,6 +770,11 @@ pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> inner: unsafe { Arc::from_inner(KBox::leak(inner).into()) }, }) } + +/// Return a raw pointer to the data in this unique arc. +pub fn as_ptr (&self) -> *const T { +Arc ::as_ptr (&self.inner) + } }impl <T >UniqueArc <MaybeUninit <T >> {
---
base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260215-unique-arc-as-ptr-32eb209dde1b
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>
Benno Lossin
reviewerQuoted 13 lines · Andreas Hindborg
Add a method to
UniqueArcfor getting a raw pointer. The implementation defers to theArcimplementation of the same method.Signed-off-by Andreas Hindborg <[email protected]>--- rust/kernel/sync/arc.rs | 5 +++++ 1 file changed, 5 insertions(+)rust/kernel/sync/arc.rs +0
-0
rust/kernel/sync/arc.rs pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> · line 775
Quoted 6 lines
rust/kernel/sync/arc.rs +3
-0@@ -770,6 +770,11 @@ pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> inner: unsafe { Arc::from_inner(KBox::leak(inner).into()) }, }) } + +/// Return a raw pointer to the data in this unique arc. +pub fn as_ptr (&self) -> *const T {
This should be an associated function instead of an inherent method.
Cheers,
Benno
Quoted 11 lines
rust/kernel/sync/arc.rs +2
-1+Arc ::as_ptr (&self.inner) + } }impl <T >UniqueArc <MaybeUninit <T >> { ---base-commit:
05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260215-unique-arc-as-ptr-32eb209dde1bBest regards,
Andreas Hindborg
reviewerQuoted 9 lines · Benno Lossin
Quoted 8 lines · Andreas Hindborg
Add a method to
UniqueArcfor getting a raw pointer. The implementation defers to theArcimplementation of the same method.Signed-off-by Andreas Hindborg <[email protected]>--- rust/kernel/sync/arc.rs | 5 +++++ 1 file changed, 5 insertions(+)
Quoted 13 lines
rust/kernel/sync/arc.rs pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> · line 775
Quoted 11 lines
rust/kernel/sync/arc.rs +3
-0@@ -770,6 +770,11 @@ pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> inner: unsafe { Arc::from_inner(KBox::leak(inner).into()) }, }) } + +/// Return a raw pointer to the data in this unique arc. +pub fn as_ptr (&self) -> *const T {This should be an associated function instead of an inherent method.
I think this is so that we always call the function on the intended object rather than going through Deref. Could you please clarify if this is correct?
Best regards,
Andreas Hindborg
Benno Lossin
reviewerQuoted 25 lines · Andreas Hindborg
Quoted 9 lines · Benno Lossin
Quoted 8 lines · Andreas Hindborg
Add a method to
UniqueArcfor getting a raw pointer. The implementation defers to theArcimplementation of the same method.Signed-off-by Andreas Hindborg <[email protected]>--- rust/kernel/sync/arc.rs | 5 +++++ 1 file changed, 5 insertions(+)Quoted 13 lines
rust/kernel/sync/arc.rs pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> · line 775
Quoted 11 lines
rust/kernel/sync/arc.rs +3
-0@@ -770,6 +770,11 @@ pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> inner: unsafe { Arc::from_inner(KBox::leak(inner).into()) }, }) } + +/// Return a raw pointer to the data in this unique arc. +pub fn as_ptr (&self) -> *const T {This should be an associated function instead of an inherent method.
Quoted 3 lines
I think this is so that we always call the function on the intended object rather than going through Deref. Could you please clarify if this is correct?
If you declare an inherent method on a type that is Deref (or Receiver), then you can never call a method with the same name on the object it derefs to:
struct MyThing;
impl MyThing {
fn as_ptr(&self) -> *const () { /* ... */ }
}
let my_thing = UniqueArc::new(MyThing);
let _: *const MyThing = my_thing.as_ptr(); // this is the method from `UniqueArc`
// to call the other one, we have to write:
let _: *const () = MyThing::my_thing(&*my_thing);Especially a function with the name as_ptr should be associated, since both very likely return a pointer; this increases the likelihood of accidentally calling the wrong one.
Cheers,
Benno
Gary Guo
maintainerQuoted 18 lines · Andreas Hindborg
Add a method to
UniqueArcfor getting a raw pointer. The implementation defers to theArcimplementation of the same method.Signed-off-by Andreas Hindborg <[email protected]>--- rust/kernel/sync/arc.rs | 5 +++++ 1 file changed, 5 insertions(+)rust/kernel/sync/arc.rs +0
-0@@ -770,6 +770,11 @@ pub fn new_uninit(flags: Flags) ->Result<UniqueArc<MaybeUninit<T>>, AllocError>
inner: unsafe { Arc::from_inner(KBox::leak(inner).into())
},}) }
rust/kernel/sync/arc.rs pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> · line 774
Quoted 2 lines
rust/kernel/sync/arc.rs +2
-0+ +/// Return a raw pointer to the data in this unique arc.
Please add an #[inline] here and make this function not a method.
Best,
Gary
Quoted 12 lines
rust/kernel/sync/arc.rs +3
-1+pub fn as_ptr (&self) -> *const T { +Arc ::as_ptr (&self.inner) + } }impl <T >UniqueArc <MaybeUninit <T >> { ---base-commit:
05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260215-unique-arc-as-ptr-32eb209dde1bBest regards,