Skip to content

Commit

Permalink
impl Needle<&[T]> for &&[T], &Box<[T]>.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytm committed Apr 19, 2019
1 parent 9dc2d70 commit f1f34fa
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
33 changes: 33 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ use core::hash::{Hash, Hasher};
use core::iter::{Iterator, FromIterator, FusedIterator};
use core::marker::{Unpin, Unsize};
use core::mem;
use core::needle::Needle;
use core::pin::Pin;
use core::ops::{
CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Receiver, Generator, GeneratorState
Expand Down Expand Up @@ -919,3 +920,35 @@ impl<F: ?Sized + Future + Unpin> Future for Box<F> {
F::poll(Pin::new(&mut *self), cx)
}
}

#[unstable(feature = "needle", issue = "56345")]
impl<'p, 'h, T: PartialEq + 'p + 'h> Needle<&'h [T]> for &'p Box<[T]> {
type Searcher = <&'p [T] as Needle<&'h [T]>>::Searcher;
type Consumer = <&'p [T] as Needle<&'h [T]>>::Consumer;

#[inline]
fn into_searcher(self) -> Self::Searcher {
<&'p [T] as Needle<&'h [T]>>::into_searcher(&**self)
}

#[inline]
fn into_consumer(self) -> Self::Consumer {
<&'p [T] as Needle<&'h [T]>>::into_consumer(&**self)
}
}

#[unstable(feature = "needle", issue = "56345")]
impl<'p, 'h, T: PartialEq + 'p + 'h> Needle<&'h mut [T]> for &'p Box<[T]> {
type Searcher = <&'p [T] as Needle<&'h mut [T]>>::Searcher;
type Consumer = <&'p [T] as Needle<&'h mut [T]>>::Consumer;

#[inline]
fn into_searcher(self) -> Self::Searcher {
<&'p [T] as Needle<&'h mut [T]>>::into_searcher(&**self)
}

#[inline]
fn into_consumer(self) -> Self::Consumer {
<&'p [T] as Needle<&'h mut [T]>>::into_consumer(&**self)
}
}
7 changes: 7 additions & 0 deletions src/liballoc/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1709,3 +1709,10 @@ fn test_replace() {
b" **E**mpowering **E**veryone to build reliable and **E**fficient software.".to_vec()
);
}

#[test]
fn test_boxed_slice_ref_is_a_needle() {
let boundary = vec![b'x'; 12].into_boxed_slice();
let bytes: &[u8] = b"--xxxxxxxxxxxxyyyy";
assert!(bytes[2..].starts_with(&boundary));
}
14 changes: 8 additions & 6 deletions src/libcore/slice/needles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,8 @@ where
}

macro_rules! impl_needle_for_slice_searcher {
(@<$haystack:ty> for $ty:ty) => {
impl<'p, 'h, T> Needle<$haystack> for $ty
([$($gen:tt)+] <$haystack:ty> for $ty:ty) => {
impl<$($gen)+, 'h, T> Needle<$haystack> for $ty
where
T: PartialEq + 'p,
{
Expand All @@ -720,11 +720,13 @@ macro_rules! impl_needle_for_slice_searcher {
};

($($index:expr),*) => {
impl_needle_for_slice_searcher!(@<&'h [T]> for &'p [T]);
impl_needle_for_slice_searcher!(@<&'h mut [T]> for &'p [T]);
impl_needle_for_slice_searcher!(['p] <&'h [T]> for &'p [T]);
impl_needle_for_slice_searcher!(['p] <&'h mut [T]> for &'p [T]);
impl_needle_for_slice_searcher!(['q, 'p] <&'h [T]> for &'q &'p [T]);
impl_needle_for_slice_searcher!(['q, 'p] <&'h mut [T]> for &'q &'p [T]);
$(
impl_needle_for_slice_searcher!(@<&'h [T]> for &'p [T; $index]);
impl_needle_for_slice_searcher!(@<&'h mut [T]> for &'p [T; $index]);
impl_needle_for_slice_searcher!(['p] <&'h [T]> for &'p [T; $index]);
impl_needle_for_slice_searcher!(['p] <&'h mut [T]> for &'p [T; $index]);
)*
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/libcore/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,3 +1475,10 @@ fn test_is_sorted() {
assert!(!["c", "bb", "aaa"].is_sorted());
assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));
}

#[test]
fn test_double_array_ref_is_a_needle() {
static MAGIC_NUMBERS: [&[u8]; 2] = [b"%PDF", b"\x89PNG"];
let buffer: &[u8] = b"%PDF123";
assert!(MAGIC_NUMBERS.iter().any(|magic| buffer.starts_with(magic)));
}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-2149.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error[E0599]: no method named `bind` found for type `[&str; 1]` in the current s
--> $DIR/issue-2149.rs:13:12
|
LL | ["hi"].bind(|x| [x] );
| ^^^^ help: did you mean: `find`
| ^^^^ help: there is a method with a similar name: `find`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `bind`, perhaps you need to implement it:
Expand Down

0 comments on commit f1f34fa

Please sign in to comment.