Skip to contentSkip to conversation

rust: sync: add `UniqueArc::as_ptr`

PATCH Superseded Andreas Hindborg 3 participants 5 messages Feb 15 – Feb 16

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

PATCH 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
index 289f77abf415a..9c70fdd39bd2f 100644
@@ -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]>
reviewer
#1 lore raw
Quoted 13 lines · Andreas Hindborg

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 +0 -0
index 289f77abf415a..9c70fdd39bd2f 100644

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-32eb209dde1b

Best regards,

author reviewer
#2 lore raw
Quoted 9 lines · Benno Lossin
Quoted 8 lines · Andreas Hindborg

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(+)
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
index 289f77abf415a..9c70fdd39bd2f 100644
@@ -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

reviewer
#3 lore raw
Quoted 25 lines · Andreas Hindborg
Quoted 9 lines · Benno Lossin
Quoted 8 lines · Andreas Hindborg

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(+)
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
index 289f77abf415a..9c70fdd39bd2f 100644
@@ -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

maintainer
#4 lore raw
In reply to Andreas Hindborg
Quoted 18 lines · Andreas Hindborg

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 +0 -0
index 289f77abf415a..9c70fdd39bd2f 100644
@@ -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-32eb209dde1b

Best regards,