rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 1/2] rust: sync: add `Arc::ptr_eq`
@ 2023-05-17 20:08 Alice Ryhl
  2023-05-17 20:08 ` [PATCH v1 2/2] rust: sync: implement `AsRef<T>` for `Arc<T>` Alice Ryhl
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Alice Ryhl @ 2023-05-17 20:08 UTC (permalink / raw)
  To: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Will Deacon, Peter Zijlstra, Mark Rutland, Alice Ryhl,
	rust-for-linux, linux-kernel, patches, Wedson Almeida Filho

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.

Co-developed-by: Wedson Almeida Filho <walmeida@microsoft.com>
Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/sync/arc.rs | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index e6d206242465..274febe3bb06 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -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


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-05-25 13:51 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-17 20:08 [PATCH v1 1/2] rust: sync: add `Arc::ptr_eq` Alice Ryhl
2023-05-17 20:08 ` [PATCH v1 2/2] rust: sync: implement `AsRef<T>` for `Arc<T>` Alice Ryhl
2023-05-17 20:42   ` Martin Rodriguez Reboredo
2023-05-23 11:59   ` Andreas Hindborg
2023-05-23 16:01   ` Gary Guo
2023-05-25 13:50   ` Benno Lossin
2023-05-17 20:41 ` [PATCH v1 1/2] rust: sync: add `Arc::ptr_eq` Martin Rodriguez Reboredo
2023-05-23 11:54 ` Andreas Hindborg
2023-05-23 16:00 ` Gary Guo
2023-05-25 13:50 ` Benno Lossin

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).