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]>
---
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
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`.
Co-developed-by: Wedson Almeida Filho <[email protected]>
Signed-off-by: Wedson Almeida Filho <[email protected]>
Signed-off-by: Alice Ryhl <[email protected]>
---
rust/kernel/sync/arc.rs | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 274febe3bb06..9ec911e4a0c7 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -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
On 5/17/23 17:08, Alice Ryhl wrote:
> [...]
> +
> + /// 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: Martin Rodriguez Reboredo <[email protected]>
On 5/17/23 17:08, Alice Ryhl wrote:
> [...]
>
> +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: Martin Rodriguez Reboredo <[email protected]>
Alice Ryhl <[email protected]> writes:
> 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`.
>
> 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: Andreas Hindborg <[email protected]>
> ---
> rust/kernel/sync/arc.rs | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 274febe3bb06..9ec911e4a0c7 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -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.
Alice Ryhl <[email protected]> writes:
> 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: Andreas Hindborg <[email protected]>
> ---
> 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
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`.
>
> 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: Gary Guo <[email protected]>
> ---
> rust/kernel/sync/arc.rs | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 274febe3bb06..9ec911e4a0c7 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -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.
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.
>
> 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: Gary Guo <[email protected]>
> ---
> 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
On 5/17/23 22:08, Alice Ryhl 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.
>
> 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]>
> ---
> 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
>
--
Cheers,
Benno
On 5/17/23 22:08, Alice Ryhl 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`.
>
> 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]>
> ---
> rust/kernel/sync/arc.rs | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 274febe3bb06..9ec911e4a0c7 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -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
>
--
Cheers,
Benno