about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorFlorian Gilcher <florian.gilcher@asquera.de>2013-10-02 15:37:59 +0200
committerFlorian Gilcher <florian.gilcher@asquera.de>2013-10-02 15:37:59 +0200
commit4dc3ff9c80bab9da65f3a8323882ec082a80bbeb (patch)
treea0230fbd562712b3ca0dcecc12d21a367a54a8c2 /src/libstd
parentd00c9269dce3a7925d7d0bf5edb64b3c6747c6af (diff)
downloadrust-4dc3ff9c80bab9da65f3a8323882ec082a80bbeb.tar.gz
rust-4dc3ff9c80bab9da65f3a8323882ec082a80bbeb.zip
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.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/str.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 6c9992b8139..56e8efe2a3c 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -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)]