Skip to content

Commit f1f34fa

Browse files
committedApr 19, 2019
impl Needle<&[T]> for &&[T], &Box<[T]>.
1 parent 9dc2d70 commit f1f34fa

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed
 

‎src/liballoc/boxed.rs

+33
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use core::hash::{Hash, Hasher};
7878
use core::iter::{Iterator, FromIterator, FusedIterator};
7979
use core::marker::{Unpin, Unsize};
8080
use core::mem;
81+
use core::needle::Needle;
8182
use core::pin::Pin;
8283
use core::ops::{
8384
CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Receiver, Generator, GeneratorState
@@ -919,3 +920,35 @@ impl<F: ?Sized + Future + Unpin> Future for Box<F> {
919920
F::poll(Pin::new(&mut *self), cx)
920921
}
921922
}
923+
924+
#[unstable(feature = "needle", issue = "56345")]
925+
impl<'p, 'h, T: PartialEq + 'p + 'h> Needle<&'h [T]> for &'p Box<[T]> {
926+
type Searcher = <&'p [T] as Needle<&'h [T]>>::Searcher;
927+
type Consumer = <&'p [T] as Needle<&'h [T]>>::Consumer;
928+
929+
#[inline]
930+
fn into_searcher(self) -> Self::Searcher {
931+
<&'p [T] as Needle<&'h [T]>>::into_searcher(&**self)
932+
}
933+
934+
#[inline]
935+
fn into_consumer(self) -> Self::Consumer {
936+
<&'p [T] as Needle<&'h [T]>>::into_consumer(&**self)
937+
}
938+
}
939+
940+
#[unstable(feature = "needle", issue = "56345")]
941+
impl<'p, 'h, T: PartialEq + 'p + 'h> Needle<&'h mut [T]> for &'p Box<[T]> {
942+
type Searcher = <&'p [T] as Needle<&'h mut [T]>>::Searcher;
943+
type Consumer = <&'p [T] as Needle<&'h mut [T]>>::Consumer;
944+
945+
#[inline]
946+
fn into_searcher(self) -> Self::Searcher {
947+
<&'p [T] as Needle<&'h mut [T]>>::into_searcher(&**self)
948+
}
949+
950+
#[inline]
951+
fn into_consumer(self) -> Self::Consumer {
952+
<&'p [T] as Needle<&'h mut [T]>>::into_consumer(&**self)
953+
}
954+
}

‎src/liballoc/tests/slice.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1709,3 +1709,10 @@ fn test_replace() {
17091709
b" **E**mpowering **E**veryone to build reliable and **E**fficient software.".to_vec()
17101710
);
17111711
}
1712+
1713+
#[test]
1714+
fn test_boxed_slice_ref_is_a_needle() {
1715+
let boundary = vec![b'x'; 12].into_boxed_slice();
1716+
let bytes: &[u8] = b"--xxxxxxxxxxxxyyyy";
1717+
assert!(bytes[2..].starts_with(&boundary));
1718+
}

‎src/libcore/slice/needles.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,8 @@ where
699699
}
700700

701701
macro_rules! impl_needle_for_slice_searcher {
702-
(@<$haystack:ty> for $ty:ty) => {
703-
impl<'p, 'h, T> Needle<$haystack> for $ty
702+
([$($gen:tt)+] <$haystack:ty> for $ty:ty) => {
703+
impl<$($gen)+, 'h, T> Needle<$haystack> for $ty
704704
where
705705
T: PartialEq + 'p,
706706
{
@@ -720,11 +720,13 @@ macro_rules! impl_needle_for_slice_searcher {
720720
};
721721

722722
($($index:expr),*) => {
723-
impl_needle_for_slice_searcher!(@<&'h [T]> for &'p [T]);
724-
impl_needle_for_slice_searcher!(@<&'h mut [T]> for &'p [T]);
723+
impl_needle_for_slice_searcher!(['p] <&'h [T]> for &'p [T]);
724+
impl_needle_for_slice_searcher!(['p] <&'h mut [T]> for &'p [T]);
725+
impl_needle_for_slice_searcher!(['q, 'p] <&'h [T]> for &'q &'p [T]);
726+
impl_needle_for_slice_searcher!(['q, 'p] <&'h mut [T]> for &'q &'p [T]);
725727
$(
726-
impl_needle_for_slice_searcher!(@<&'h [T]> for &'p [T; $index]);
727-
impl_needle_for_slice_searcher!(@<&'h mut [T]> for &'p [T; $index]);
728+
impl_needle_for_slice_searcher!(['p] <&'h [T]> for &'p [T; $index]);
729+
impl_needle_for_slice_searcher!(['p] <&'h mut [T]> for &'p [T; $index]);
728730
)*
729731
}
730732
}

‎src/libcore/tests/slice.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1475,3 +1475,10 @@ fn test_is_sorted() {
14751475
assert!(!["c", "bb", "aaa"].is_sorted());
14761476
assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));
14771477
}
1478+
1479+
#[test]
1480+
fn test_double_array_ref_is_a_needle() {
1481+
static MAGIC_NUMBERS: [&[u8]; 2] = [b"%PDF", b"\x89PNG"];
1482+
let buffer: &[u8] = b"%PDF123";
1483+
assert!(MAGIC_NUMBERS.iter().any(|magic| buffer.starts_with(magic)));
1484+
}

‎src/test/ui/issues/issue-2149.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0599]: no method named `bind` found for type `[&str; 1]` in the current s
1010
--> $DIR/issue-2149.rs:13:12
1111
|
1212
LL | ["hi"].bind(|x| [x] );
13-
| ^^^^ help: did you mean: `find`
13+
| ^^^^ help: there is a method with a similar name: `find`
1414
|
1515
= help: items from traits can only be used if the trait is implemented and in scope
1616
= note: the following trait defines an item `bind`, perhaps you need to implement it:

0 commit comments

Comments
 (0)
Please sign in to comment.