Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be written as:

        self.iter().zip(other.iter()).all(|(x, y)| x == y)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I mindlessly copied the other impl, but as written it's not clear the bounds check for other[i] would always be elided. I've updated both impls to user iterators now.

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
Expand Down Expand Up @@ -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)*) => {
Expand Down