From 0284903ff67a892f6776a1ac62ead93d8b419d52 Mon Sep 17 00:00:00 2001 From: Adam Kern Date: Sun, 14 Dec 2025 16:52:49 -0500 Subject: [PATCH] Add PartialEq implementations between ArrayRef and ArrayBase This will allow users to ergonomically compare the two types without over-verbose dereferences. --- src/arraytraits.rs | 108 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 3 deletions(-) diff --git a/src/arraytraits.rs b/src/arraytraits.rs index da87e3a58..aaba07b0f 100644 --- a/src/arraytraits.rs +++ b/src/arraytraits.rs @@ -207,7 +207,6 @@ where /// Return `true` if the array shapes and all elements of `self` and /// `rhs` are equal. Return `false` otherwise. -#[allow(clippy::unconditional_recursion)] // false positive impl PartialEq<&ArrayBase> for ArrayBase where A: PartialEq, @@ -223,7 +222,6 @@ where /// Return `true` if the array shapes and all elements of `self` and /// `rhs` are equal. Return `false` otherwise. -#[allow(clippy::unconditional_recursion)] // false positive impl PartialEq> for &ArrayBase where A: PartialEq, @@ -236,7 +234,6 @@ where **self == *rhs } } - impl Eq for ArrayBase where D: Dimension, @@ -245,6 +242,78 @@ where { } +impl PartialEq> for ArrayBase +where + S: Data, + A: PartialEq, + D: Dimension, +{ + fn eq(&self, other: &ArrayRef) -> bool + { + **self == other + } +} + +impl PartialEq<&ArrayRef> for ArrayBase +where + S: Data, + A: PartialEq, + D: Dimension, +{ + fn eq(&self, other: &&ArrayRef) -> bool + { + **self == *other + } +} + +impl PartialEq> for &ArrayBase +where + S: Data, + A: PartialEq, + D: Dimension, +{ + fn eq(&self, other: &ArrayRef) -> bool + { + **self == other + } +} + +impl PartialEq> for ArrayRef +where + S: Data, + A: PartialEq, + D: Dimension, +{ + fn eq(&self, other: &ArrayBase) -> bool + { + self == **other + } +} + +impl PartialEq<&ArrayBase> for ArrayRef +where + S: Data, + A: PartialEq, + D: Dimension, +{ + fn eq(&self, other: &&ArrayBase) -> bool + { + self == ***other + } +} + +impl PartialEq> for &ArrayRef +where + S: Data, + A: PartialEq, + D: Dimension, +{ + fn eq(&self, other: &ArrayBase) -> bool + { + *self == **other + } +} + impl From> for ArrayBase where S: DataOwned { @@ -643,3 +712,36 @@ where ArrayBase::default(D::default()) } } + +#[cfg(test)] +mod tests +{ + use crate::array; + + #[test] + fn test_eq_traits() + { + let a = array![1, 2, 3]; + let a_ref = &*a; + let b = array![1, 2, 3]; + let b_ref = &*b; + + assert_eq!(a, b); + assert_eq!(a, &b); + assert_eq!(&a, b); + assert_eq!(&a, &b); + + assert_eq!(a_ref, b_ref); + assert_eq!(&a_ref, b_ref); + assert_eq!(a_ref, &b_ref); + assert_eq!(&a_ref, &b_ref); + + assert_eq!(a_ref, b); + assert_eq!(a_ref, &b); + assert_eq!(&a_ref, &b); + + assert_eq!(a, b_ref); + assert_eq!(&a, b_ref); + assert_eq!(&a, &b_ref); + } +}