Skip to content

Commit

Permalink
Remove auto-deref'ing Pattern impl because it conflicts with other
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikomatsakis committed Mar 23, 2015
1 parent 8bd8466 commit 76ead08
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
20 changes: 20 additions & 0 deletions src/libcollections/string.rs
Expand Up @@ -25,6 +25,7 @@ use core::mem;
use core::ops::{self, Deref, Add, Index};
use core::ptr;
use core::slice;
use core::str::Pattern;
use unicode::str as unicode_str;
use unicode::str::Utf16Item;

Expand Down Expand Up @@ -765,6 +766,25 @@ impl<'a> Extend<&'a str> for String {
}
}

/// A convenience impl that delegates to the impl for `&str`
impl<'a, 'b> Pattern<'a> for &'b String {
type Searcher = <&'b str as Pattern<'a>>::Searcher;

fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<'a>>::Searcher {
self[..].into_searcher(haystack)
}

#[inline]
fn is_contained_in(self, haystack: &'a str) -> bool {
self[..].is_contained_in(haystack)
}

#[inline]
fn is_prefix_of(self, haystack: &'a str) -> bool {
self[..].is_prefix_of(haystack)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for String {
#[inline]
Expand Down
20 changes: 7 additions & 13 deletions src/libcore/str/pattern.rs
Expand Up @@ -474,22 +474,16 @@ impl<'a, 'b> Pattern<'a> for &'b [char] {
s, CharEqPattern(s));
}

/// A convenience impl that delegates to the impl for `&str`
impl<'a, 'b> Pattern<'a> for &'b &'b str {
type Searcher = <&'b str as Pattern<'a>>::Searcher;
associated_items!(<&'b str as Pattern<'a>>::Searcher,
s, (*s));
}

/// Searches for chars that match the given predicate
impl<'a, F> Pattern<'a> for F where F: FnMut(char) -> bool {
type Searcher = <CharEqPattern<Self> as Pattern<'a>>::Searcher;
associated_items!(<CharEqPattern<Self> as Pattern<'a>>::Searcher,
s, CharEqPattern(s));
}

// Deref-forward impl

use ops::Deref;

/// Delegates to the next deref coercion of `Self` that implements `Pattern`
impl<'a, 'b, P: 'b + ?Sized, T: Deref<Target = P> + ?Sized> Pattern<'a> for &'b T
where &'b P: Pattern<'a>
{
type Searcher = <&'b P as Pattern<'a>>::Searcher;
associated_items!(<&'b P as Pattern<'a>>::Searcher,
s, (&**s));
}
2 changes: 0 additions & 2 deletions src/libcoretest/str.rs
Expand Up @@ -13,9 +13,7 @@ fn test_pattern_deref_forward() {
let data = "aabcdaa";
assert!(data.contains("bcd"));
assert!(data.contains(&"bcd"));
assert!(data.contains(&&"bcd"));
assert!(data.contains(&"bcd".to_string()));
assert!(data.contains(&&"bcd".to_string()));
}

#[test]
Expand Down

0 comments on commit 76ead08

Please sign in to comment.