Skip to content

Commit ccd9a96

Browse files
committed
auto merge of #9681 : skade/rust/str-from-str, r=huonw
This fixes an issue for APIs that return FromStr. Before this change, they would have to offer a seperate function for returning the source string.
2 parents 2076073 + 4dc3ff9 commit ccd9a96

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/libstd/str.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ use option::{None, Option, Some};
105105
use ptr;
106106
use ptr::RawPtr;
107107
use to_str::ToStr;
108+
use from_str::FromStr;
108109
use uint;
109110
use vec;
110111
use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector};
@@ -204,6 +205,11 @@ impl ToStr for ~str {
204205
fn to_str(&self) -> ~str { self.to_owned() }
205206
}
206207

208+
impl FromStr for ~str {
209+
#[inline]
210+
fn from_str(s: &str) -> Option<~str> { Some(s.to_owned()) }
211+
}
212+
207213
impl<'self> ToStr for &'self str {
208214
#[inline]
209215
fn to_str(&self) -> ~str { self.to_owned() }
@@ -214,6 +220,11 @@ impl ToStr for @str {
214220
fn to_str(&self) -> ~str { self.to_owned() }
215221
}
216222

223+
impl<'self> FromStr for @str {
224+
#[inline]
225+
fn from_str(s: &str) -> Option<@str> { Some(s.to_managed()) }
226+
}
227+
217228
/// Convert a byte to a UTF-8 string
218229
///
219230
/// # Failure
@@ -2580,13 +2591,14 @@ impl Default for @str {
25802591
#[cfg(test)]
25812592
mod tests {
25822593
use container::Container;
2583-
use option::{None, Some};
2594+
use option::{None, Some, Option};
25842595
use ptr;
25852596
use str::*;
25862597
use vec;
25872598
use vec::{Vector, ImmutableVector, CopyableVector};
25882599
use cmp::{TotalOrd, Less, Equal, Greater};
25892600
use send_str::{SendStrOwned, SendStrStatic};
2601+
use from_str::from_str;
25902602
25912603
#[test]
25922604
fn test_eq() {
@@ -3889,6 +3901,14 @@ mod tests {
38893901
assert_eq!("abcde".to_send_str(), SendStrStatic("abcde"));
38903902
assert_eq!("abcde".to_send_str(), SendStrOwned(~"abcde"));
38913903
}
3904+
3905+
#[test]
3906+
fn test_from_str() {
3907+
let owned: Option<~str> = from_str(&"string");
3908+
assert_eq!(owned, Some(~"string"));
3909+
let managed: Option<@str> = from_str(&"string");
3910+
assert_eq!(managed, Some(@"string"));
3911+
}
38923912
}
38933913
38943914
#[cfg(test)]

0 commit comments

Comments
 (0)