From 5dc13b35c110cd13228ff2bfb670c6a8e4167780 Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Sun, 17 Jul 2022 11:18:07 +0200 Subject: [PATCH 1/2] Vec - PartialEq where Rhs is the Vec --- src/vec.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/vec.rs b/src/vec.rs index 0d9c3244..2d76d96a 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -1024,6 +1024,16 @@ where } } +// [B] == Vec +impl PartialEq> for [B] +where + A: PartialEq, +{ + fn eq(&self, other: &Vec) -> bool { + <[A]>::eq(other, &self[..]) + } +} + // Vec == &[B] impl PartialEq<&[B]> for Vec where @@ -1034,6 +1044,16 @@ where } } +// &[B] == Vec +impl PartialEq> for &[B] +where + A: PartialEq, +{ + fn eq(&self, other: &Vec) -> bool { + <[A]>::eq(other, &self[..]) + } +} + // Vec == &mut [B] impl PartialEq<&mut [B]> for Vec where @@ -1044,6 +1064,16 @@ where } } +// &mut [B] == Vec +impl PartialEq> for &mut [B] +where + A: PartialEq, +{ + fn eq(&self, other: &Vec) -> bool { + <[A]>::eq(other, &self[..]) + } +} + // Vec == [B; M] // Equality does not require equal capacity impl PartialEq<[B; M]> for Vec @@ -1055,6 +1085,17 @@ where } } +// [B; M] == Vec +// Equality does not require equal capacity +impl PartialEq> for [B; M] +where + A: PartialEq, +{ + fn eq(&self, other: &Vec) -> bool { + <[A]>::eq(other, &self[..]) + } +} + // Vec == &[B; M] // Equality does not require equal capacity impl PartialEq<&[B; M]> for Vec @@ -1066,6 +1107,17 @@ where } } +// &[B; M] == Vec +// Equality does not require equal capacity +impl PartialEq> for &[B; M] +where + A: PartialEq, +{ + fn eq(&self, other: &Vec) -> bool { + <[A]>::eq(other, &self[..]) + } +} + // Implements Eq if underlying data is Eq impl Eq for Vec where T: Eq {} @@ -1239,6 +1291,28 @@ mod tests { assert!(xs < ys); } + #[test] + fn cmp_with_arrays_and_slices() { + let mut xs: Vec = Vec::new(); + xs.push(1).unwrap(); + + let array = [1]; + + assert_eq!(xs, array); + assert_eq!(array, xs); + + assert_eq!(xs, array.as_slice()); + assert_eq!(array.as_slice(), xs); + + assert_eq!(xs, &array); + assert_eq!(&array, xs); + + let longer_array = [1; 20]; + + assert_ne!(xs, longer_array); + assert_ne!(longer_array, xs); + } + #[test] fn full() { let mut v: Vec = Vec::new(); From e3e3bd42fdce00c398f816fbfca9c4400608116c Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 9 Aug 2022 11:31:33 +0200 Subject: [PATCH 2/2] applease rustfmt --- src/vec.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vec.rs b/src/vec.rs index 2d76d96a..4e46df16 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -1300,10 +1300,10 @@ mod tests { assert_eq!(xs, array); assert_eq!(array, xs); - + assert_eq!(xs, array.as_slice()); assert_eq!(array.as_slice(), xs); - + assert_eq!(xs, &array); assert_eq!(&array, xs);