Skip to content

Commit 76ead08

Browse files
committed
Remove auto-deref'ing Pattern impl because it conflicts with other
possible blanket impls and also triggers internal overflow. Add some special cases for common uses (&&str, &String) for now; bounds-targeting deref coercions are probably the right longer term answer.
1 parent 8bd8466 commit 76ead08

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

src/libcollections/string.rs

+20
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use core::mem;
2525
use core::ops::{self, Deref, Add, Index};
2626
use core::ptr;
2727
use core::slice;
28+
use core::str::Pattern;
2829
use unicode::str as unicode_str;
2930
use unicode::str::Utf16Item;
3031

@@ -765,6 +766,25 @@ impl<'a> Extend<&'a str> for String {
765766
}
766767
}
767768

769+
/// A convenience impl that delegates to the impl for `&str`
770+
impl<'a, 'b> Pattern<'a> for &'b String {
771+
type Searcher = <&'b str as Pattern<'a>>::Searcher;
772+
773+
fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<'a>>::Searcher {
774+
self[..].into_searcher(haystack)
775+
}
776+
777+
#[inline]
778+
fn is_contained_in(self, haystack: &'a str) -> bool {
779+
self[..].is_contained_in(haystack)
780+
}
781+
782+
#[inline]
783+
fn is_prefix_of(self, haystack: &'a str) -> bool {
784+
self[..].is_prefix_of(haystack)
785+
}
786+
}
787+
768788
#[stable(feature = "rust1", since = "1.0.0")]
769789
impl PartialEq for String {
770790
#[inline]

src/libcore/str/pattern.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -474,22 +474,16 @@ impl<'a, 'b> Pattern<'a> for &'b [char] {
474474
s, CharEqPattern(s));
475475
}
476476

477+
/// A convenience impl that delegates to the impl for `&str`
478+
impl<'a, 'b> Pattern<'a> for &'b &'b str {
479+
type Searcher = <&'b str as Pattern<'a>>::Searcher;
480+
associated_items!(<&'b str as Pattern<'a>>::Searcher,
481+
s, (*s));
482+
}
483+
477484
/// Searches for chars that match the given predicate
478485
impl<'a, F> Pattern<'a> for F where F: FnMut(char) -> bool {
479486
type Searcher = <CharEqPattern<Self> as Pattern<'a>>::Searcher;
480487
associated_items!(<CharEqPattern<Self> as Pattern<'a>>::Searcher,
481488
s, CharEqPattern(s));
482489
}
483-
484-
// Deref-forward impl
485-
486-
use ops::Deref;
487-
488-
/// Delegates to the next deref coercion of `Self` that implements `Pattern`
489-
impl<'a, 'b, P: 'b + ?Sized, T: Deref<Target = P> + ?Sized> Pattern<'a> for &'b T
490-
where &'b P: Pattern<'a>
491-
{
492-
type Searcher = <&'b P as Pattern<'a>>::Searcher;
493-
associated_items!(<&'b P as Pattern<'a>>::Searcher,
494-
s, (&**s));
495-
}

src/libcoretest/str.rs

-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ fn test_pattern_deref_forward() {
1313
let data = "aabcdaa";
1414
assert!(data.contains("bcd"));
1515
assert!(data.contains(&"bcd"));
16-
assert!(data.contains(&&"bcd"));
1716
assert!(data.contains(&"bcd".to_string()));
18-
assert!(data.contains(&&"bcd".to_string()));
1917
}
2018

2119
#[test]

0 commit comments

Comments
 (0)