From 2ecaef2adf7ca0e7fa10603cd10a70177e680e1f Mon Sep 17 00:00:00 2001 From: Stepan Tubanov Date: Sat, 5 Mar 2022 16:29:10 +0300 Subject: [PATCH] Add PartialEq implementations --- src/array_string.rs | 14 ++++++++++++++ src/arrayvec.rs | 32 +++++++++++++++++++------------- tests/tests.rs | 33 ++++++++++++++++++++++++++------- 3 files changed, 59 insertions(+), 20 deletions(-) diff --git a/src/array_string.rs b/src/array_string.rs index c4712a0..5530824 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -457,6 +457,13 @@ impl PartialEq for ArrayString } } +impl PartialEq<&str> for ArrayString +{ + fn eq(&self, rhs: &&str) -> bool { + &**self == *rhs + } +} + impl PartialEq> for str { fn eq(&self, rhs: &ArrayString) -> bool { @@ -464,6 +471,13 @@ impl PartialEq> for str } } +impl PartialEq> for &str +{ + fn eq(&self, rhs: &ArrayString) -> bool { + *self == &**rhs + } +} + impl Eq for ArrayString { } diff --git a/src/arrayvec.rs b/src/arrayvec.rs index e69e60c..f9859e2 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -1132,21 +1132,27 @@ impl Hash for ArrayVec } } -impl PartialEq for ArrayVec - where T: PartialEq -{ - fn eq(&self, other: &Self) -> bool { - **self == **other - } +macro_rules! impl_slice_eq { + ([$($vars:tt)*] $lhs:ty, $rhs:ty) => { + impl PartialEq<$rhs> for $lhs + where T: PartialEq { + #[inline] + fn eq(&self, other: &$rhs) -> bool { + self[..] == other[..] + } + } + }; } -impl PartialEq<[T]> for ArrayVec - where T: PartialEq -{ - fn eq(&self, other: &[T]) -> bool { - **self == *other - } -} +impl_slice_eq!([const C0: usize, const C1: usize] ArrayVec, ArrayVec); +impl_slice_eq!([const C: usize] ArrayVec, &[U]); +impl_slice_eq!([const C: usize] ArrayVec, &mut [U]); +impl_slice_eq!([const C: usize] &[T], ArrayVec); +impl_slice_eq!([const C: usize] &mut [T], ArrayVec); +impl_slice_eq!([const C: usize] [T], ArrayVec); +impl_slice_eq!([const C: usize] ArrayVec, [U]); +impl_slice_eq!([const C: usize, const N: usize] ArrayVec, [U; N]); +impl_slice_eq!([const C: usize, const N: usize] ArrayVec, &[U; N]); impl Eq for ArrayVec where T: Eq { } diff --git a/tests/tests.rs b/tests/tests.rs index 2f8a5ef..09108db 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -47,9 +47,9 @@ fn test_extend_from_slice() { vec.try_extend_from_slice(&[1, 2, 3]).unwrap(); assert_eq!(vec.len(), 3); - assert_eq!(&vec[..], &[1, 2, 3]); + assert_eq!(vec, [1, 2, 3]); assert_eq!(vec.pop(), Some(3)); - assert_eq!(&vec[..], &[1, 2]); + assert_eq!(vec, [1, 2]); } #[test] @@ -528,8 +528,8 @@ fn test_string() { let text = "hello world"; let mut s = ArrayString::<16>::new(); s.try_push_str(text).unwrap(); - assert_eq!(&s, text); - assert_eq!(text, &s); + assert_eq!(s, text); + assert_eq!(text, s); // Make sure Hash / Eq / Borrow match up so we can use HashMap let mut map = HashMap::new(); @@ -611,7 +611,7 @@ fn test_insert_at_length() { let result1 = v.try_insert(0, "a"); let result2 = v.try_insert(1, "b"); assert!(result1.is_ok() && result2.is_ok()); - assert_eq!(&v[..], &["a", "b"]); + assert_eq!(v, ["a", "b"]); } #[should_panic] @@ -736,6 +736,15 @@ fn test_try_from_argument() { assert_eq!(&v, "Hello 123"); } +#[test] +fn test_nested_eq() { + let array: ArrayVec, 5> = (0..3) + .map(|i| ArrayVec::from([i, i + 1])) + .collect(); + + assert_eq!(array, [[0, 1], [1, 2], [2, 3]]); +} + #[test] fn allow_max_capacity_arrayvec_type() { // this type is allowed to be used (but can't be constructed) @@ -768,7 +777,7 @@ fn test_arrayvec_const_constructible() { let mut var = OF_U8; assert!(var.is_empty()); - assert_eq!(var, ArrayVec::new()); + assert_eq!(var, ArrayVec::, 10>::new()); var.push(vec![3, 5, 8]); assert_eq!(var[..], [vec![3, 5, 8]]); } @@ -790,4 +799,14 @@ fn test_arraystring_zero_filled_has_some_sanity_checks() { let string = ArrayString::<4>::zero_filled(); assert_eq!(string.as_str(), "\0\0\0\0"); assert_eq!(string.len(), 4); -} \ No newline at end of file +} + +#[test] +fn test_vec_of_strings_equality() { + let vec = ArrayVec::from([ + ArrayString::<8>::from("one").unwrap(), + ArrayString::<8>::from("two").unwrap(), + ArrayString::<8>::from("three").unwrap(), + ]); + assert_eq!(vec, ["one", "two", "three"]); +}