Skip to contentSkip to conversation

rust: sync: add `Arc::ptr_eq`

PATCH In review Alice Ryhl 5 participants 10 messages May 17, 2023 – May 25, 2023

Subsystems (2)
lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

PATCH 1/2 rust: sync: add `Arc::ptr_eq`

Add a method for comparing whether two Arc pointers reference the same underlying object.

This comparison can already be done by getting a reference to the inner values and comparing whether the references have the same address. However, writing Arc::ptr_eq(a, b) is generally less error-prone than doing the same check on the references, since you might otherwise accidentally compare the two &Arc<T> references instead, which wont work because those are pointers to pointers to the inner value, when you just want to compare the pointers to the inner value.

Also, this method might optimize better because getting a reference to the inner value involves offsetting the pointer, which this method does not need to do.

---
 rust/kernel/sync/arc.rs | 5 +++++
 1 file changed, 5 insertions(+)
rust/kernel/sync/arc.rs +5 -0
index e6d206242465..274febe3bb06 100644
@@ -221,6 +221,11 @@ impl<T: ?Sized> Arc<T> {
         // reference can be created.
         unsafe { ArcBorrow::new(self.ptr) }
     }
+
+    /// Compare whether two [`Arc`] pointers reference the same underlying object.
+    pub fn ptr_eq(this: &Self, other: &Self) -> bool {
+        core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
+    }
 }
 
 impl<T: 'static> ForeignOwnable for Arc<T> {

base-commit: ac9a78681b921877518763ba0e89202254349d1b

Signature · 1 line
2.40.1.606.ga4b1b128d6-goog
author maintainer
#1 lore raw

PATCH 2/2 rust: sync: implement `AsRef<T>` for `Arc<T>`

This trait lets you use Arc<T> in code that is generic over smart pointer types.

The AsRef trait should be implemented on all smart pointers. The standard library also implements it on the ordinary Arc.

---
 rust/kernel/sync/arc.rs | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/sync/arc.rs +6 -0
index 274febe3bb06..9ec911e4a0c7 100644
@@ -264,6 +264,12 @@ impl<T: ?Sized> Deref for Arc<T> {
     }
 }
 
+impl<T: ?Sized> AsRef<T> for Arc<T> {
+    fn as_ref(&self) -> &T {
+        self.deref()
+    }
+}
+
 impl<T: ?Sized> Clone for Arc<T> {
     fn clone(&self) -> Self {
         // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
Signature · 1 line
2.40.1.606.ga4b1b128d6-goog
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH v1 2/2] rust: sync: implement `AsRef<T>` for `Arc<T>`

rust/kernel/sync/arc.rs impl<T: ?Sized> Deref for Arc<T> { · line 275

Quoted 11 lines · Alice Ryhl

[...]

rust/kernel/sync/arc.rs +6 -0
+impl<T: ?Sized> AsRef<T> for Arc<T> {
+    fn as_ref(&self) -> &T {
+        self.deref()
+    }
+}
+
  impl<T: ?Sized> Clone for Arc<T> {
      fn clone(&self) -> Self {
          // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH v1 2/2] rust: sync: implement `AsRef<T>` for `Arc<T>`

In reply to Alice Ryhl
Quoted 9 lines · Alice Ryhl

This trait lets you use Arc<T> in code that is generic over smart pointer types.

The AsRef trait should be implemented on all smart pointers. The standard library also implements it on the ordinary Arc.

Quoted 21 lines
---
 rust/kernel/sync/arc.rs | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/sync/arc.rs +6 -0
index 274febe3bb06..9ec911e4a0c7 100644
@@ -264,6 +264,12 @@ impl<T: ?Sized> Deref for Arc<T> {
     }
 }
 
+impl<T: ?Sized> AsRef<T> for Arc<T> {
+    fn as_ref(&self) -> &T {
+        self.deref()
+    }
+}
+
 impl<T: ?Sized> Clone for Arc<T> {
     fn clone(&self) -> Self {
         // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
maintainer Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH v1 2/2] rust: sync: implement `AsRef<T>` for `Arc<T>`

In reply to Alice Ryhl
Quoted 9 lines · On Wed, 17 May 2023 20:08:14 +0000 Alice Ryhl <[email protected]> wrote:

This trait lets you use Arc<T> in code that is generic over smart pointer types.

The AsRef trait should be implemented on all smart pointers. The standard library also implements it on the ordinary Arc.

Quoted 21 lines
---
 rust/kernel/sync/arc.rs | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/sync/arc.rs +6 -0
index 274febe3bb06..9ec911e4a0c7 100644
@@ -264,6 +264,12 @@ impl<T: ?Sized> Deref for Arc<T> {
     }
 }
 
+impl<T: ?Sized> AsRef<T> for Arc<T> {
+    fn as_ref(&self) -> &T {
+        self.deref()
+    }
+}
+
 impl<T: ?Sized> Clone for Arc<T> {
     fn clone(&self) -> Self {
         // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH v1 2/2] rust: sync: implement `AsRef<T>` for `Arc<T>`

In reply to Alice Ryhl
Quoted 9 lines · Alice Ryhl

This trait lets you use Arc<T> in code that is generic over smart pointer types.

The AsRef trait should be implemented on all smart pointers. The standard library also implements it on the ordinary Arc.

Quoted 24 lines
---
  rust/kernel/sync/arc.rs | 6 ++++++
  1 file changed, 6 insertions(+)
rust/kernel/sync/arc.rs +6 -1
index 274febe3bb06..9ec911e4a0c7 100644
@@ -264,6 +264,12 @@ impl<T: ?Sized> Deref for Arc<T> {
      }
  }
 
+impl<T: ?Sized> AsRef<T> for Arc<T> {
+    fn as_ref(&self) -> &T {
+        self.deref()
+    }
+}
+
  impl<T: ?Sized> Clone for Arc<T> {
      fn clone(&self) -> Self {
          // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
--

2.40.1.606.ga4b1b128d6-goog

Signature · 2 lines
Cheers,
Benno
Reviewed-by on v1 1/2
#6 lore raw
In reply to Alice Ryhl
Quoted 11 lines · Alice Ryhl

[...]

rust/kernel/sync/arc.rs +5 -0
+
+    /// Compare whether two [`Arc`] pointers reference the same underlying object.
+    pub fn ptr_eq(this: &Self, other: &Self) -> bool {
+        core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
+    }
  }
 
  impl<T: 'static> ForeignOwnable for Arc<T> {

base-commit: ac9a78681b921877518763ba0e89202254349d1b

Reviewed-by on v1 1/2
#7 lore raw
In reply to Alice Ryhl
Quoted 11 lines · Alice Ryhl

Add a method for comparing whether two Arc pointers reference the same underlying object.

This comparison can already be done by getting a reference to the inner values and comparing whether the references have the same address. However, writing Arc::ptr_eq(a, b) is generally less error-prone than doing the same check on the references, since you might otherwise accidentally compare the two &Arc<T> references instead, which wont work because those are pointers to pointers to the inner value, when you just want to compare the pointers to the inner value.

Quoted 7 lines

Also, this method might optimize better because getting a reference to the inner value involves offsetting the pointer, which this method does not need to do.

Quoted 22 lines
---
 rust/kernel/sync/arc.rs | 5 +++++
 1 file changed, 5 insertions(+)
rust/kernel/sync/arc.rs +5 -0
index e6d206242465..274febe3bb06 100644
@@ -221,6 +221,11 @@ impl<T: ?Sized> Arc<T> {
         // reference can be created.
         unsafe { ArcBorrow::new(self.ptr) }
     }
+
+    /// Compare whether two [`Arc`] pointers reference the same underlying object.
+    pub fn ptr_eq(this: &Self, other: &Self) -> bool {
+        core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
+    }
 }
 
 impl<T: 'static> ForeignOwnable for Arc<T> {

base-commit: ac9a78681b921877518763ba0e89202254349d1b

maintainer Reviewed-by on v1 1/2
#8 lore raw
In reply to Alice Ryhl
Quoted 11 lines · On Wed, 17 May 2023 20:08:13 +0000 Alice Ryhl <[email protected]> wrote:

Add a method for comparing whether two Arc pointers reference the same underlying object.

This comparison can already be done by getting a reference to the inner values and comparing whether the references have the same address. However, writing Arc::ptr_eq(a, b) is generally less error-prone than doing the same check on the references, since you might otherwise accidentally compare the two &Arc<T> references instead, which wont work because those are pointers to pointers to the inner value, when you just want to compare the pointers to the inner value.

Quoted 7 lines

Also, this method might optimize better because getting a reference to the inner value involves offsetting the pointer, which this method does not need to do.

Quoted 22 lines
---
 rust/kernel/sync/arc.rs | 5 +++++
 1 file changed, 5 insertions(+)
rust/kernel/sync/arc.rs +5 -0
index e6d206242465..274febe3bb06 100644
@@ -221,6 +221,11 @@ impl<T: ?Sized> Arc<T> {
         // reference can be created.
         unsafe { ArcBorrow::new(self.ptr) }
     }
+
+    /// Compare whether two [`Arc`] pointers reference the same underlying object.
+    pub fn ptr_eq(this: &Self, other: &Self) -> bool {
+        core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
+    }
 }
 
 impl<T: 'static> ForeignOwnable for Arc<T> {

base-commit: ac9a78681b921877518763ba0e89202254349d1b

Reviewed-by on v1 1/2
#9 lore raw
In reply to Alice Ryhl
Quoted 11 lines · Alice Ryhl

Add a method for comparing whether two Arc pointers reference the same underlying object.

This comparison can already be done by getting a reference to the inner values and comparing whether the references have the same address. However, writing Arc::ptr_eq(a, b) is generally less error-prone than doing the same check on the references, since you might otherwise accidentally compare the two &Arc<T> references instead, which wont work because those are pointers to pointers to the inner value, when you just want to compare the pointers to the inner value.

Quoted 7 lines

Also, this method might optimize better because getting a reference to the inner value involves offsetting the pointer, which this method does not need to do.

Quoted 25 lines
---
  rust/kernel/sync/arc.rs | 5 +++++
  1 file changed, 5 insertions(+)
rust/kernel/sync/arc.rs +5 -0
index e6d206242465..274febe3bb06 100644
@@ -221,6 +221,11 @@ impl<T: ?Sized> Arc<T> {
          // reference can be created.
          unsafe { ArcBorrow::new(self.ptr) }
      }
+
+    /// Compare whether two [`Arc`] pointers reference the same underlying object.
+    pub fn ptr_eq(this: &Self, other: &Self) -> bool {
+        core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
+    }
  }
 
  impl<T: 'static> ForeignOwnable for Arc<T> {

base-commit: ac9a78681b921877518763ba0e89202254349d1b --
2.40.1.606.ga4b1b128d6-goog

Signature · 2 lines
Cheers,
Benno