Skip to content

Commit 4334d3c

Browse files
committed
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)
2 parents bad1062 + 02720a4 commit 4334d3c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/libcore/str.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,14 +1277,19 @@ pub mod traits {
12771277
}
12781278

12791279
/// Any string that can be represented as a slice
1280-
pub trait Str {
1280+
pub trait Str for Sized? {
12811281
/// Work with `self` as a slice.
12821282
fn as_slice<'a>(&'a self) -> &'a str;
12831283
}
12841284

1285-
impl<'a> Str for &'a str {
1285+
impl Str for str {
12861286
#[inline]
1287-
fn as_slice<'a>(&'a self) -> &'a str { *self }
1287+
fn as_slice<'a>(&'a self) -> &'a str { self }
1288+
}
1289+
1290+
impl<'a, Sized? S> Str for &'a S where S: Str {
1291+
#[inline]
1292+
fn as_slice(&self) -> &str { Str::as_slice(*self) }
12881293
}
12891294

12901295
/// Methods for string slices

0 commit comments

Comments
 (0)