diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2021-12-11 15:29:52 -0800 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2021-12-14 13:15:15 -0800 |
| commit | a0b96902e4c622d40c7186fc0c7ba13efc1fc912 (patch) | |
| tree | c19077465a770d87052fce16fefe419ab3036c0c /library/core | |
| parent | 404c8471aba60c2d837fa728e7c729a0f52d5830 (diff) | |
Do array-slice equality via arrays, rather than always via slices
This'll still go via slices eventually for large arrays, but this way slice comparisons to short arrays can use the same memcmp-avoidance tricks. Added some tests for all the combinations to make sure I didn't accidentally infinitely-recurse something.
Diffstat (limited to 'library/core')
| -rw-r--r-- | library/core/src/array/equality.rs | 41 | ||||
| -rw-r--r-- | library/core/tests/array.rs | 44 |
2 files changed, 73 insertions, 12 deletions
diff --git a/library/core/src/array/equality.rs b/library/core/src/array/equality.rs index 25e056501be..33f7f494e9d 100644 --- a/library/core/src/array/equality.rs +++ b/library/core/src/array/equality.rs @@ -1,3 +1,4 @@ +use crate::convert::TryInto; use crate::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize}; use crate::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize}; @@ -23,11 +24,19 @@ where { #[inline] fn eq(&self, other: &[B]) -> bool { - self[..] == other[..] + let b: Result<&[B; N], _> = other.try_into(); + match b { + Ok(b) => *self == *b, + Err(_) => false, + } } #[inline] fn ne(&self, other: &[B]) -> bool { - self[..] != other[..] + let b: Result<&[B; N], _> = other.try_into(); + match b { + Ok(b) => *self != *b, + Err(_) => true, + } } } @@ -38,11 +47,19 @@ where { #[inline] fn eq(&self, other: &[A; N]) -> bool { - self[..] == other[..] + let b: Result<&[B; N], _> = self.try_into(); + match b { + Ok(b) => *b == *other, + Err(_) => false, + } } #[inline] fn ne(&self, other: &[A; N]) -> bool { - self[..] != other[..] + let b: Result<&[B; N], _> = self.try_into(); + match b { + Ok(b) => *b != *other, + Err(_) => true, + } } } @@ -53,11 +70,11 @@ where { #[inline] fn eq(&self, other: &&[B]) -> bool { - self[..] == other[..] + *self == **other } #[inline] fn ne(&self, other: &&[B]) -> bool { - self[..] != other[..] + *self != **other } } @@ -68,11 +85,11 @@ where { #[inline] fn eq(&self, other: &[A; N]) -> bool { - self[..] == other[..] + **self == *other } #[inline] fn ne(&self, other: &[A; N]) -> bool { - self[..] != other[..] + **self != *other } } @@ -83,11 +100,11 @@ where { #[inline] fn eq(&self, other: &&mut [B]) -> bool { - self[..] == other[..] + *self == **other } #[inline] fn ne(&self, other: &&mut [B]) -> bool { - self[..] != other[..] + *self != **other } } @@ -98,11 +115,11 @@ where { #[inline] fn eq(&self, other: &[A; N]) -> bool { - self[..] == other[..] + **self == *other } #[inline] fn ne(&self, other: &[A; N]) -> bool { - self[..] != other[..] + **self != *other } } diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs index ceb67200796..c799df01a4a 100644 --- a/library/core/tests/array.rs +++ b/library/core/tests/array.rs @@ -624,3 +624,47 @@ fn array_intoiter_advance_back_by() { assert_eq!(it.len(), 0); assert_eq!(counter.get(), 100); } + +#[test] +fn array_mixed_equality_integers() { + let array3: [i32; 3] = [1, 2, 3]; + let array3b: [i32; 3] = [3, 2, 1]; + let array4: [i32; 4] = [1, 2, 3, 4]; + + let slice3: &[i32] = &{ array3 }; + let slice3b: &[i32] = &{ array3b }; + let slice4: &[i32] = &{ array4 }; + assert!(array3 == slice3); + assert!(array3 != slice3b); + assert!(array3 != slice4); + assert!(slice3 == array3); + assert!(slice3b != array3); + assert!(slice4 != array3); + + let mut3: &mut [i32] = &mut { array3 }; + let mut3b: &mut [i32] = &mut { array3b }; + let mut4: &mut [i32] = &mut { array4 }; + assert!(array3 == mut3); + assert!(array3 != mut3b); + assert!(array3 != mut4); + assert!(mut3 == array3); + assert!(mut3b != array3); + assert!(mut4 != array3); +} + +#[test] +fn array_mixed_equality_nans() { + let array3: [f32; 3] = [1.0, std::f32::NAN, 3.0]; + + let slice3: &[f32] = &{ array3 }; + assert!(!(array3 == slice3)); + assert!(array3 != slice3); + assert!(!(slice3 == array3)); + assert!(slice3 != array3); + + let mut3: &mut [f32] = &mut { array3 }; + assert!(!(array3 == mut3)); + assert!(array3 != mut3); + assert!(!(mut3 == array3)); + assert!(mut3 != array3); +} |
