auto merge of #9892 : Kimundi/rust/ResultToStr, r=alexcrichton
This commit is contained in:
commit
fa03c94546
@ -22,6 +22,7 @@
|
||||
use vec::OwnedVector;
|
||||
use to_str::ToStr;
|
||||
use str::StrSlice;
|
||||
use fmt;
|
||||
|
||||
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
|
||||
///
|
||||
@ -290,7 +291,7 @@ pub trait AsResult<T, E> {
|
||||
|
||||
impl<T: Clone, E> option::ToOption<T> for Result<T, E> {
|
||||
#[inline]
|
||||
fn to_option(&self)-> Option<T> {
|
||||
fn to_option(&self) -> Option<T> {
|
||||
match *self {
|
||||
Ok(ref t) => Some(t.clone()),
|
||||
Err(_) => None,
|
||||
@ -300,7 +301,7 @@ fn to_option(&self)-> Option<T> {
|
||||
|
||||
impl<T, E> option::IntoOption<T> for Result<T, E> {
|
||||
#[inline]
|
||||
fn into_option(self)-> Option<T> {
|
||||
fn into_option(self) -> Option<T> {
|
||||
match self {
|
||||
Ok(t) => Some(t),
|
||||
Err(_) => None,
|
||||
@ -310,7 +311,7 @@ fn into_option(self)-> Option<T> {
|
||||
|
||||
impl<T, E> option::AsOption<T> for Result<T, E> {
|
||||
#[inline]
|
||||
fn as_option<'a>(&'a self)-> Option<&'a T> {
|
||||
fn as_option<'a>(&'a self) -> Option<&'a T> {
|
||||
match *self {
|
||||
Ok(ref t) => Some(t),
|
||||
Err(_) => None,
|
||||
@ -340,7 +341,7 @@ fn as_result<'a>(&'a self) -> Result<&'a T, &'a E> {
|
||||
|
||||
impl<T: Clone, E: Clone> either::ToEither<E, T> for Result<T, E> {
|
||||
#[inline]
|
||||
fn to_either(&self)-> either::Either<E, T> {
|
||||
fn to_either(&self) -> either::Either<E, T> {
|
||||
match *self {
|
||||
Ok(ref t) => either::Right(t.clone()),
|
||||
Err(ref e) => either::Left(e.clone()),
|
||||
@ -350,7 +351,7 @@ fn to_either(&self)-> either::Either<E, T> {
|
||||
|
||||
impl<T, E> either::IntoEither<E, T> for Result<T, E> {
|
||||
#[inline]
|
||||
fn into_either(self)-> either::Either<E, T> {
|
||||
fn into_either(self) -> either::Either<E, T> {
|
||||
match self {
|
||||
Ok(t) => either::Right(t),
|
||||
Err(e) => either::Left(e),
|
||||
@ -360,7 +361,7 @@ fn into_either(self)-> either::Either<E, T> {
|
||||
|
||||
impl<T, E> either::AsEither<E, T> for Result<T, E> {
|
||||
#[inline]
|
||||
fn as_either<'a>(&'a self)-> either::Either<&'a E, &'a T> {
|
||||
fn as_either<'a>(&'a self) -> either::Either<&'a E, &'a T> {
|
||||
match *self {
|
||||
Ok(ref t) => either::Right(t),
|
||||
Err(ref e) => either::Left(e),
|
||||
@ -368,6 +369,26 @@ fn as_either<'a>(&'a self)-> either::Either<&'a E, &'a T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToStr, E: ToStr> ToStr for Result<T, E> {
|
||||
#[inline]
|
||||
fn to_str(&self) -> ~str {
|
||||
match *self {
|
||||
Ok(ref t) => format!("Ok({:s})", t.to_str()),
|
||||
Err(ref e) => format!("Err({:s})", e.to_str())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Default, E: fmt::Default> fmt::Default for Result<T, E> {
|
||||
#[inline]
|
||||
fn fmt(s: &Result<T, E>, f: &mut fmt::Formatter) {
|
||||
match *s {
|
||||
Ok(ref t) => write!(f.buf, "Ok({})", *t),
|
||||
Err(ref e) => write!(f.buf, "Err({})", *e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes each element in the iterator: if it is an error, no further
|
||||
/// elements are taken, and the error is returned.
|
||||
/// Should no error occur, a vector containing the values of each Result
|
||||
@ -441,6 +462,8 @@ mod tests {
|
||||
use option;
|
||||
use str::OwnedStr;
|
||||
use vec::ImmutableVector;
|
||||
use to_str::ToStr;
|
||||
use fmt::Default;
|
||||
|
||||
pub fn op1() -> Result<int, ~str> { Ok(666) }
|
||||
pub fn op2() -> Result<int, ~str> { Err(~"sadface") }
|
||||
@ -659,4 +682,22 @@ pub fn test_as_either() {
|
||||
assert_eq!(ok.as_either().unwrap_right(), &100);
|
||||
assert_eq!(err.as_either().unwrap_left(), &404);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_to_str() {
|
||||
let ok: Result<int, ~str> = Ok(100);
|
||||
let err: Result<int, ~str> = Err(~"Err");
|
||||
|
||||
assert_eq!(ok.to_str(), ~"Ok(100)");
|
||||
assert_eq!(err.to_str(), ~"Err(Err)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_fmt_default() {
|
||||
let ok: Result<int, ~str> = Ok(100);
|
||||
let err: Result<int, ~str> = Err(~"Err");
|
||||
|
||||
assert_eq!(format!("{}", ok), ~"Ok(100)");
|
||||
assert_eq!(format!("{}", err), ~"Err(Err)");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user