rust: sync: add `Arc::ptr_eq`
Alice Ryhl
maintainerPATCH 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
@@ -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
Alice Ryhl
maintainerPATCH 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
@@ -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
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.
Re: [PATCH v1 2/2] rust: sync: implement `AsRef<T>` for `Arc<T>`
In reply to Alice RyhlQuoted 9 lines · Alice Ryhl
This trait lets you use
Arc<T>in code that is generic over smart pointer types.The
AsReftrait should be implemented on all smart pointers. The standard library also implements it on the ordinaryArc.Co-developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Quoted 21 lines
--- rust/kernel/sync/arc.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/sync/arc.rs +6
-0@@ -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.
Re: [PATCH v1 2/2] rust: sync: implement `AsRef<T>` for `Arc<T>`
In reply to Alice RyhlQuoted 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
AsReftrait should be implemented on all smart pointers. The standard library also implements it on the ordinaryArc.Co-developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Quoted 21 lines
--- rust/kernel/sync/arc.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/sync/arc.rs +6
-0@@ -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.
Re: [PATCH v1 2/2] rust: sync: implement `AsRef<T>` for `Arc<T>`
In reply to Alice RyhlQuoted 9 lines · Alice Ryhl
This trait lets you use
Arc<T>in code that is generic over smart pointer types.The
AsReftrait should be implemented on all smart pointers. The standard library also implements it on the ordinaryArc.Co-developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Quoted 24 lines
--- rust/kernel/sync/arc.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/sync/arc.rs +6
-1@@ -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
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
Quoted 11 lines · Alice Ryhl
Add a method for comparing whether two
Arcpointers 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.
Co-developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Quoted 22 lines
--- rust/kernel/sync/arc.rs | 5 +++++ 1 file changed, 5 insertions(+)rust/kernel/sync/arc.rs +5
-0@@ -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
Quoted 11 lines · On Wed, 17 May 2023 20:08:13 +0000 Alice Ryhl <[email protected]> wrote:
Add a method for comparing whether two
Arcpointers 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.
Co-developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Quoted 22 lines
--- rust/kernel/sync/arc.rs | 5 +++++ 1 file changed, 5 insertions(+)rust/kernel/sync/arc.rs +5
-0@@ -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
Quoted 11 lines · Alice Ryhl
Add a method for comparing whether two
Arcpointers 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.
Co-developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Quoted 25 lines
--- rust/kernel/sync/arc.rs | 5 +++++ 1 file changed, 5 insertions(+)rust/kernel/sync/arc.rs +5
-0@@ -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