[PATCH 1/3] rust: sync: implement == operator for ARef

Alice Ryhl posted 3 patches 1 month, 1 week ago
There is a newer version of this series
[PATCH 1/3] rust: sync: implement == operator for ARef
Posted by Alice Ryhl 1 month, 1 week ago
Rust Binder wants to perform a comparison between ARef<Task> and &Task,
so define the == operator for ARef<_> when compared with another ARef<_>
or just a reference. The operator is implemented in terms of the same
operator applied to the inner type.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/sync/aref.rs | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
index 0616c0353c2b3b4d8b3be6c4b8156995a2f1d504..f56598a847e949b497f4cf2a05a7298a58a0ccef 100644
--- a/rust/kernel/sync/aref.rs
+++ b/rust/kernel/sync/aref.rs
@@ -170,3 +170,26 @@ fn drop(&mut self) {
         unsafe { T::dec_ref(self.ptr) };
     }
 }
+
+impl<T, U> PartialEq<ARef<U>> for ARef<T>
+where
+    T: AlwaysRefCounted + PartialEq<U>,
+    U: AlwaysRefCounted,
+{
+    #[inline]
+    fn eq(&self, other: &ARef<U>) -> bool {
+        T::eq(&**self, &**other)
+    }
+}
+
+impl<T, U> PartialEq<&'_ U> for ARef<T>
+where
+    T: AlwaysRefCounted + PartialEq<U>,
+{
+    #[inline]
+    fn eq(&self, other: &&U) -> bool {
+        T::eq(&**self, other)
+    }
+}
+
+impl<T: AlwaysRefCounted + Eq> Eq for ARef<T> {}

-- 
2.53.0.335.g19a08e0c02-goog
Re: [PATCH 1/3] rust: sync: implement == operator for ARef
Posted by Gary Guo 1 month, 1 week ago
On 2026-02-19 13:52, Alice Ryhl wrote:
> Rust Binder wants to perform a comparison between ARef<Task> and &Task,
> so define the == operator for ARef<_> when compared with another 
> ARef<_>
> or just a reference. The operator is implemented in terms of the same
> operator applied to the inner type.
> 
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

I suppose `PartialEq<U>` cannot be implemented due to trait coherence 
issue?

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/kernel/sync/aref.rs | 23 +++++++++++++++++++++++
>  1 file changed, 23 insertions(+)
> 
> diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
> index 
> 0616c0353c2b3b4d8b3be6c4b8156995a2f1d504..f56598a847e949b497f4cf2a05a7298a58a0ccef 
> 100644
> --- a/rust/kernel/sync/aref.rs
> +++ b/rust/kernel/sync/aref.rs
> @@ -170,3 +170,26 @@ fn drop(&mut self) {
>          unsafe { T::dec_ref(self.ptr) };
>      }
>  }
> +
> +impl<T, U> PartialEq<ARef<U>> for ARef<T>
> +where
> +    T: AlwaysRefCounted + PartialEq<U>,
> +    U: AlwaysRefCounted,
> +{
> +    #[inline]
> +    fn eq(&self, other: &ARef<U>) -> bool {
> +        T::eq(&**self, &**other)
> +    }
> +}
> +
> +impl<T, U> PartialEq<&'_ U> for ARef<T>
> +where
> +    T: AlwaysRefCounted + PartialEq<U>,
> +{
> +    #[inline]
> +    fn eq(&self, other: &&U) -> bool {
> +        T::eq(&**self, other)
> +    }
> +}
> +
> +impl<T: AlwaysRefCounted + Eq> Eq for ARef<T> {}
Re: [PATCH 1/3] rust: sync: implement == operator for ARef
Posted by Alice Ryhl 1 month, 1 week ago
On Thu, Feb 19, 2026 at 02:27:38PM +0000, Gary Guo wrote:
> On 2026-02-19 13:52, Alice Ryhl wrote:
> > Rust Binder wants to perform a comparison between ARef<Task> and &Task,
> > so define the == operator for ARef<_> when compared with another ARef<_>
> > or just a reference. The operator is implemented in terms of the same
> > operator applied to the inner type.
> > 
> > Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> 
> I suppose `PartialEq<U>` cannot be implemented due to trait coherence issue?

That's right:

error[E0119]: conflicting implementations of trait `core::cmp::PartialEq<sync::aref::ARef<_>>` for type `sync::aref::ARef<_>`
   --> /usr/local/google/home/aliceryhl/devel/linux/rust/kernel/sync/aref.rs:185:1
    |
174 | / impl<T, U> PartialEq<ARef<U>> for ARef<T>
175 | | where
176 | |     T: AlwaysRefCounted + PartialEq<U>,
177 | |     U: AlwaysRefCounted,
    | |________________________- first implementation here
...
185 | / impl<T, U> PartialEq<U> for ARef<T>
186 | | where
187 | |     T: AlwaysRefCounted + PartialEq<U>,
    | |_______________________________________^ conflicting implementation for `sync::aref::ARef<_>`

I can mention this in commit msg if I send another version.

> Reviewed-by: Gary Guo <gary@garyguo.net>

Thanks!