Skip to content

Commit bd780ae

Browse files
Darksonnojeda
authored andcommitted
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]>
1 parent e37b654 commit bd780ae

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

rust/kernel/sync/arc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ impl<T: ?Sized> Arc<T> {
221221
// reference can be created.
222222
unsafe { ArcBorrow::new(self.ptr) }
223223
}
224+
225+
/// Compare whether two [`Arc`] pointers reference the same underlying object.
226+
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
227+
core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
228+
}
224229
}
225230

226231
impl<T: 'static> ForeignOwnable for Arc<T> {

0 commit comments

Comments
 (0)