Skip to content

Commit 39fafd6

Browse files
committed
auto merge of #8204 : kballard/rust/str-into-owned, r=graydon
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. I don't have any concrete uses for this yet, since the one conversion I've done to `&[S]` so far (see PR #8203) didn't actually need owned strings. But having this here may make using `Str` more attractive. It also may be worth adding an `into_managed()` function, but that one is less obviously useful than `into_owned()`.
2 parents 20fad0f + aa94dfa commit 39fafd6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libstd/str.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1079,25 +1079,37 @@ pub mod traits {}
10791079
pub trait Str {
10801080
/// Work with `self` as a slice.
10811081
fn as_slice<'a>(&'a self) -> &'a str;
1082+
1083+
/// Convert `self` into a ~str.
1084+
fn into_owned(self) -> ~str;
10821085
}
10831086
10841087
impl<'self> Str for &'self str {
10851088
#[inline]
10861089
fn as_slice<'a>(&'a self) -> &'a str { *self }
1090+
1091+
#[inline]
1092+
fn into_owned(self) -> ~str { self.to_owned() }
10871093
}
10881094
10891095
impl<'self> Str for ~str {
10901096
#[inline]
10911097
fn as_slice<'a>(&'a self) -> &'a str {
10921098
let s: &'a str = *self; s
10931099
}
1100+
1101+
#[inline]
1102+
fn into_owned(self) -> ~str { self }
10941103
}
10951104
10961105
impl<'self> Str for @str {
10971106
#[inline]
10981107
fn as_slice<'a>(&'a self) -> &'a str {
10991108
let s: &'a str = *self; s
11001109
}
1110+
1111+
#[inline]
1112+
fn into_owned(self) -> ~str { self.to_owned() }
11011113
}
11021114
11031115
impl<'self> Container for &'self str {

0 commit comments

Comments
 (0)