Skip to content

Commit f8f2888

Browse files
committed
Use memchr in [u8]::contains
1 parent 2bf0df7 commit f8f2888

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/libcore/slice/mod.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ impl<T> SliceExt for [T] {
624624

625625
#[inline]
626626
fn contains(&self, x: &T) -> bool where T: PartialEq {
627-
self.iter().any(|elt| *x == *elt)
627+
x.slice_contains(self)
628628
}
629629

630630
#[inline]
@@ -2619,3 +2619,19 @@ unsafe impl<'a, T> TrustedRandomAccess for IterMut<'a, T> {
26192619
}
26202620
fn may_have_side_effect() -> bool { false }
26212621
}
2622+
2623+
trait SliceContains: Sized {
2624+
fn slice_contains(&self, x: &[Self]) -> bool;
2625+
}
2626+
2627+
impl<T> SliceContains for T where T: PartialEq {
2628+
default fn slice_contains(&self, x: &[Self]) -> bool {
2629+
x.iter().any(|y| *y == *self)
2630+
}
2631+
}
2632+
2633+
impl SliceContains for u8 {
2634+
fn slice_contains(&self, x: &[Self]) -> bool {
2635+
memchr::memchr(*self, x).is_some()
2636+
}
2637+
}

0 commit comments

Comments
 (0)