From a0b96902e4c622d40c7186fc0c7ba13efc1fc912 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 11 Dec 2021 15:29:52 -0800 Subject: 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. --- library/core/src/array/equality.rs | 41 +++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'library/core/src/array') 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 } } -- cgit 1.4.1-3-g733a5