str: Add method .into_owned(self) -> ~str to Str
The method .into_owned() is meant to be used as an optimization when you need to get a ~str from a Str, but don't want to unnecessarily copy it if it's already a ~str. This is meant to ease functions that look like fn foo<S: Str>(strs: &[S]) Previously they could work with the strings as slices using .as_slice(), but producing ~str required copying the string, even if the vector turned out be a &[~str] already.
This commit is contained in:
@@ -1079,11 +1079,17 @@ pub mod traits {}
|
|||||||
pub trait Str {
|
pub trait Str {
|
||||||
/// Work with `self` as a slice.
|
/// Work with `self` as a slice.
|
||||||
fn as_slice<'a>(&'a self) -> &'a str;
|
fn as_slice<'a>(&'a self) -> &'a str;
|
||||||
|
|
||||||
|
/// Convert `self` into a ~str.
|
||||||
|
fn into_owned(self) -> ~str;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'self> Str for &'self str {
|
impl<'self> Str for &'self str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_slice<'a>(&'a self) -> &'a str { *self }
|
fn as_slice<'a>(&'a self) -> &'a str { *self }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn into_owned(self) -> ~str { self.to_owned() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'self> Str for ~str {
|
impl<'self> Str for ~str {
|
||||||
@@ -1091,6 +1097,9 @@ impl<'self> Str for ~str {
|
|||||||
fn as_slice<'a>(&'a self) -> &'a str {
|
fn as_slice<'a>(&'a self) -> &'a str {
|
||||||
let s: &'a str = *self; s
|
let s: &'a str = *self; s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn into_owned(self) -> ~str { self }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'self> Str for @str {
|
impl<'self> Str for @str {
|
||||||
@@ -1098,6 +1107,9 @@ impl<'self> Str for @str {
|
|||||||
fn as_slice<'a>(&'a self) -> &'a str {
|
fn as_slice<'a>(&'a self) -> &'a str {
|
||||||
let s: &'a str = *self; s
|
let s: &'a str = *self; s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn into_owned(self) -> ~str { self.to_owned() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'self> Container for &'self str {
|
impl<'self> Container for &'self str {
|
||||||
|
|||||||
Reference in New Issue
Block a user