diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 34efe1cfbf1..47e0c099c98 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -22,6 +22,7 @@ use vec; 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 { impl option::ToOption for Result { #[inline] - fn to_option(&self)-> Option { + fn to_option(&self) -> Option { match *self { Ok(ref t) => Some(t.clone()), Err(_) => None, @@ -300,7 +301,7 @@ impl option::ToOption for Result { impl option::IntoOption for Result { #[inline] - fn into_option(self)-> Option { + fn into_option(self) -> Option { match self { Ok(t) => Some(t), Err(_) => None, @@ -310,7 +311,7 @@ impl option::IntoOption for Result { impl option::AsOption for Result { #[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 @@ impl AsResult for Result { impl either::ToEither for Result { #[inline] - fn to_either(&self)-> either::Either { + fn to_either(&self) -> either::Either { match *self { Ok(ref t) => either::Right(t.clone()), Err(ref e) => either::Left(e.clone()), @@ -350,7 +351,7 @@ impl either::ToEither for Result { impl either::IntoEither for Result { #[inline] - fn into_either(self)-> either::Either { + fn into_either(self) -> either::Either { match self { Ok(t) => either::Right(t), Err(e) => either::Left(e), @@ -360,7 +361,7 @@ impl either::IntoEither for Result { impl either::AsEither for Result { #[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 @@ impl either::AsEither for Result { } } +impl ToStr for Result { + #[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 fmt::Default for Result { + #[inline] + fn fmt(s: &Result, 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 { Ok(666) } pub fn op2() -> Result { Err(~"sadface") } @@ -659,4 +682,22 @@ mod tests { 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 = Ok(100); + let err: Result = 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 = Ok(100); + let err: Result = Err(~"Err"); + + assert_eq!(format!("{}", ok), ~"Ok(100)"); + assert_eq!(format!("{}", err), ~"Err(Err)"); + } }