diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2017-12-13 09:11:42 -0600 |
|---|---|---|
| committer | Manish Goregaokar <manishsmail@gmail.com> | 2017-12-13 09:11:42 -0600 |
| commit | f8f28886e0d98c9cbd6cb3a719f9014960ec1d24 (patch) | |
| tree | e9cc3ab266d58f96945dec5afcf388f229ab8853 | |
| parent | 2bf0df777b7712d1b719cd5ac7cce63176b7384c (diff) | |
| download | rust-f8f28886e0d98c9cbd6cb3a719f9014960ec1d24.tar.gz rust-f8f28886e0d98c9cbd6cb3a719f9014960ec1d24.zip | |
Use memchr in [u8]::contains
| -rw-r--r-- | src/libcore/slice/mod.rs | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index e4da1b7e5f5..346ee273311 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -624,7 +624,7 @@ impl<T> SliceExt for [T] { #[inline] fn contains(&self, x: &T) -> bool where T: PartialEq { - self.iter().any(|elt| *x == *elt) + x.slice_contains(self) } #[inline] @@ -2619,3 +2619,19 @@ unsafe impl<'a, T> TrustedRandomAccess for IterMut<'a, T> { } fn may_have_side_effect() -> bool { false } } + +trait SliceContains: Sized { + fn slice_contains(&self, x: &[Self]) -> bool; +} + +impl<T> SliceContains for T where T: PartialEq { + default fn slice_contains(&self, x: &[Self]) -> bool { + x.iter().any(|y| *y == *self) + } +} + +impl SliceContains for u8 { + fn slice_contains(&self, x: &[Self]) -> bool { + memchr::memchr(*self, x).is_some() + } +} |
