Add an implementation of FromStr for ~str and @str

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.
This commit is contained in:
Florian Gilcher 2013-10-02 15:37:59 +02:00
parent d00c9269dc
commit 4dc3ff9c80

View File

@ -105,6 +105,7 @@ use option::{None, Option, Some};
use ptr;
use ptr::RawPtr;
use to_str::ToStr;
use from_str::FromStr;
use uint;
use vec;
use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector};
@ -204,6 +205,11 @@ impl ToStr for ~str {
fn to_str(&self) -> ~str { self.to_owned() }
}
impl FromStr for ~str {
#[inline]
fn from_str(s: &str) -> Option<~str> { Some(s.to_owned()) }
}
impl<'self> ToStr for &'self str {
#[inline]
fn to_str(&self) -> ~str { self.to_owned() }
@ -214,6 +220,11 @@ impl ToStr for @str {
fn to_str(&self) -> ~str { self.to_owned() }
}
impl<'self> FromStr for @str {
#[inline]
fn from_str(s: &str) -> Option<@str> { Some(s.to_managed()) }
}
/// Convert a byte to a UTF-8 string
///
/// # Failure
@ -2580,13 +2591,14 @@ impl Default for @str {
#[cfg(test)]
mod tests {
use container::Container;
use option::{None, Some};
use option::{None, Some, Option};
use ptr;
use str::*;
use vec;
use vec::{Vector, ImmutableVector, CopyableVector};
use cmp::{TotalOrd, Less, Equal, Greater};
use send_str::{SendStrOwned, SendStrStatic};
use from_str::from_str;
#[test]
fn test_eq() {
@ -3889,6 +3901,14 @@ mod tests {
assert_eq!("abcde".to_send_str(), SendStrStatic("abcde"));
assert_eq!("abcde".to_send_str(), SendStrOwned(~"abcde"));
}
#[test]
fn test_from_str() {
let owned: Option<~str> = from_str(&"string");
assert_eq!(owned, Some(~"string"));
let managed: Option<@str> = from_str(&"string");
assert_eq!(managed, Some(@"string"));
}
}
#[cfg(test)]