aboutsummaryrefslogtreecommitdiff
path: root/rust/kernel
diff options
context:
space:
mode:
authorAlice Ryhl <[email protected]>2023-05-17 20:08:13 +0000
committerMiguel Ojeda <[email protected]>2023-05-31 18:53:10 +0200
commitbd780aea671eda9660837c05d9ba58de22ceecad (patch)
tree19532e18be30bf6eb4cde2866e17d911b7f4868b /rust/kernel
parente37b654c379e514357ccadb22c6291d3471fa5e5 (diff)
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. Co-developed-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
Diffstat (limited to 'rust/kernel')
-rw-r--r--rust/kernel/sync/arc.rs5
1 files changed, 5 insertions, 0 deletions
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 1b0734fdf6a7..b03d84f7ce87 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> {