diff options
| author | Andrew Champion <andrew.champion@gmail.com> | 2019-06-08 16:26:34 +0100 |
|---|---|---|
| committer | Andrew Champion <andrew.champion@gmail.com> | 2019-06-08 16:26:34 +0100 |
| commit | 30b27f350ccca8f2b08b54b256f7e1fdb8c8ddb0 (patch) | |
| tree | 98742040b4713b89dd82a25c9bb6cf64b2e2c109 | |
| parent | 7f90abe3aa1864e40e3d516b936c4a1a84e72aee (diff) | |
| download | rust-30b27f350ccca8f2b08b54b256f7e1fdb8c8ddb0.tar.gz rust-30b27f350ccca8f2b08b54b256f7e1fdb8c8ddb0.zip | |
core: check for pointer equality when comparing Eq slices
Because Eq types must be reflexively equal, an equal-length slice to the same memory location must be equal.
| -rw-r--r-- | src/libcore/slice/mod.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 0e782bef39d..f972d13f7c3 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -5304,6 +5304,29 @@ impl<A, B> SlicePartialEq<B> for [A] } } +// Use an equal-pointer optimization when types are `Eq` +impl<A> SlicePartialEq<A> for [A] + where A: PartialEq<A> + Eq +{ + default fn equal(&self, other: &[A]) -> bool { + if self.len() != other.len() { + return false; + } + + if self.as_ptr() == other.as_ptr() { + return true; + } + + for i in 0..self.len() { + if !self[i].eq(&other[i]) { + return false; + } + } + + true + } +} + // Use memcmp for bytewise equality when the types allow impl<A> SlicePartialEq<A> for [A] where A: PartialEq<A> + BytewiseEquality @@ -5409,7 +5432,7 @@ impl SliceOrd<u8> for [u8] { #[doc(hidden)] /// Trait implemented for types that can be compared for equality using /// their bytewise representation -trait BytewiseEquality { } +trait BytewiseEquality: Eq + Copy { } macro_rules! impl_marker_for { ($traitname:ident, $($ty:ty)*) => { |
