Skip to content

Commit 0284903

Browse files
committed
Add PartialEq implementations between ArrayRef and ArrayBase
This will allow users to ergonomically compare the two types without over-verbose dereferences.
1 parent 0e1d04b commit 0284903

File tree

1 file changed

+105
-3
lines changed

1 file changed

+105
-3
lines changed

src/arraytraits.rs

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ where
207207

208208
/// Return `true` if the array shapes and all elements of `self` and
209209
/// `rhs` are equal. Return `false` otherwise.
210-
#[allow(clippy::unconditional_recursion)] // false positive
211210
impl<A, B, S, S2, D> PartialEq<&ArrayBase<S2, D>> for ArrayBase<S, D>
212211
where
213212
A: PartialEq<B>,
@@ -223,7 +222,6 @@ where
223222

224223
/// Return `true` if the array shapes and all elements of `self` and
225224
/// `rhs` are equal. Return `false` otherwise.
226-
#[allow(clippy::unconditional_recursion)] // false positive
227225
impl<A, B, S, S2, D> PartialEq<ArrayBase<S2, D>> for &ArrayBase<S, D>
228226
where
229227
A: PartialEq<B>,
@@ -236,7 +234,6 @@ where
236234
**self == *rhs
237235
}
238236
}
239-
240237
impl<S, D> Eq for ArrayBase<S, D>
241238
where
242239
D: Dimension,
@@ -245,6 +242,78 @@ where
245242
{
246243
}
247244

245+
impl<A, B, S, D> PartialEq<ArrayRef<B, D>> for ArrayBase<S, D>
246+
where
247+
S: Data<Elem = A>,
248+
A: PartialEq<B>,
249+
D: Dimension,
250+
{
251+
fn eq(&self, other: &ArrayRef<B, D>) -> bool
252+
{
253+
**self == other
254+
}
255+
}
256+
257+
impl<A, B, S, D> PartialEq<&ArrayRef<B, D>> for ArrayBase<S, D>
258+
where
259+
S: Data<Elem = A>,
260+
A: PartialEq<B>,
261+
D: Dimension,
262+
{
263+
fn eq(&self, other: &&ArrayRef<B, D>) -> bool
264+
{
265+
**self == *other
266+
}
267+
}
268+
269+
impl<A, B, S, D> PartialEq<ArrayRef<B, D>> for &ArrayBase<S, D>
270+
where
271+
S: Data<Elem = A>,
272+
A: PartialEq<B>,
273+
D: Dimension,
274+
{
275+
fn eq(&self, other: &ArrayRef<B, D>) -> bool
276+
{
277+
**self == other
278+
}
279+
}
280+
281+
impl<A, B, S, D> PartialEq<ArrayBase<S, D>> for ArrayRef<A, D>
282+
where
283+
S: Data<Elem = B>,
284+
A: PartialEq<B>,
285+
D: Dimension,
286+
{
287+
fn eq(&self, other: &ArrayBase<S, D>) -> bool
288+
{
289+
self == **other
290+
}
291+
}
292+
293+
impl<A, B, S, D> PartialEq<&ArrayBase<S, D>> for ArrayRef<A, D>
294+
where
295+
S: Data<Elem = B>,
296+
A: PartialEq<B>,
297+
D: Dimension,
298+
{
299+
fn eq(&self, other: &&ArrayBase<S, D>) -> bool
300+
{
301+
self == ***other
302+
}
303+
}
304+
305+
impl<A, B, S, D> PartialEq<ArrayBase<S, D>> for &ArrayRef<A, D>
306+
where
307+
S: Data<Elem = B>,
308+
A: PartialEq<B>,
309+
D: Dimension,
310+
{
311+
fn eq(&self, other: &ArrayBase<S, D>) -> bool
312+
{
313+
*self == **other
314+
}
315+
}
316+
248317
impl<A, S> From<Box<[A]>> for ArrayBase<S, Ix1>
249318
where S: DataOwned<Elem = A>
250319
{
@@ -643,3 +712,36 @@ where
643712
ArrayBase::default(D::default())
644713
}
645714
}
715+
716+
#[cfg(test)]
717+
mod tests
718+
{
719+
use crate::array;
720+
721+
#[test]
722+
fn test_eq_traits()
723+
{
724+
let a = array![1, 2, 3];
725+
let a_ref = &*a;
726+
let b = array![1, 2, 3];
727+
let b_ref = &*b;
728+
729+
assert_eq!(a, b);
730+
assert_eq!(a, &b);
731+
assert_eq!(&a, b);
732+
assert_eq!(&a, &b);
733+
734+
assert_eq!(a_ref, b_ref);
735+
assert_eq!(&a_ref, b_ref);
736+
assert_eq!(a_ref, &b_ref);
737+
assert_eq!(&a_ref, &b_ref);
738+
739+
assert_eq!(a_ref, b);
740+
assert_eq!(a_ref, &b);
741+
assert_eq!(&a_ref, &b);
742+
743+
assert_eq!(a, b_ref);
744+
assert_eq!(&a, b_ref);
745+
assert_eq!(&a, &b_ref);
746+
}
747+
}

0 commit comments

Comments
 (0)