Skip to content

Add an implementation of FromStr for ~str and @str #9681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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() }
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)]
Expand Down