From 76ead081088d7cc88a4686210b72b60ae12bd7a8 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sat, 21 Mar 2015 06:02:56 -0400 Subject: [PATCH] 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. --- src/libcollections/string.rs | 20 ++++++++++++++++++++ src/libcore/str/pattern.rs | 20 +++++++------------- src/libcoretest/str.rs | 2 -- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index cd6f27bf65f6..7eccd5fa3b60 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -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; @@ -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] diff --git a/src/libcore/str/pattern.rs b/src/libcore/str/pattern.rs index 7bf248917a5c..98b6533980dd 100644 --- a/src/libcore/str/pattern.rs +++ b/src/libcore/str/pattern.rs @@ -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 = as Pattern<'a>>::Searcher; associated_items!( 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 + ?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)); -} diff --git a/src/libcoretest/str.rs b/src/libcoretest/str.rs index cac78363dd53..38cab4f0e094 100644 --- a/src/libcoretest/str.rs +++ b/src/libcoretest/str.rs @@ -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]