about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-11-24 11:56:34 +0000
committerbors <bors@rust-lang.org>2014-11-24 11:56:34 +0000
commit4334d3c19699c65ba8cb354f84fa40e4b678bfa6 (patch)
treef172709f8cc46aac873f07e226c4821831ad66e4
parentbad1062caaaefe0963d7b8513786c8283e74f1e7 (diff)
parent02720a4a1665ebdca6159f5189115bdf3087afbf (diff)
auto merge of #19248 : japaric/rust/str, r=alexcrichton
Just like we do with AsSlice

This comes in handy when dealing with iterator-centric APIs (`IntoIterator`!) and you want to receive an `Iterator<S> where S: Str` argument. Without this PR, e.g. you can't receive `&["a", "b"].iter()` instead you'll have to type `&["a", "b"].iter().map(|&x| x)` (A similar thing happens with `&[String]`).

r? @aturon 

Full disclaimer: I haven't run `make`/`make check` yet (All my cores are busy)
-rw-r--r--src/libcore/str.rs11
1 files changed, 8 insertions, 3 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index 3b2a7c62312..bb7710c2acc 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -1277,14 +1277,19 @@ pub mod traits {
 }
 
 /// Any string that can be represented as a slice
-pub trait Str {
+pub trait Str for Sized? {
     /// Work with `self` as a slice.
     fn as_slice<'a>(&'a self) -> &'a str;
 }
 
-impl<'a> Str for &'a str {
+impl Str for str {
     #[inline]
-    fn as_slice<'a>(&'a self) -> &'a str { *self }
+    fn as_slice<'a>(&'a self) -> &'a str { self }
+}
+
+impl<'a, Sized? S> Str for &'a S where S: Str {
+    #[inline]
+    fn as_slice(&self) -> &str { Str::as_slice(*self) }
 }
 
 /// Methods for string slices