about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMarvin Löbel <loebel.marvin@gmail.com>2013-10-16 20:35:45 +0200
committerMarvin Löbel <loebel.marvin@gmail.com>2013-10-16 20:36:38 +0200
commitabecd61a230d7057aeb1dc6d82e3c8101c7d9337 (patch)
tree44cca14204f0ce02aec347c055206f9a371d97a1 /src
parentfabec998e5667d651d3475c12ee25ab97d21105c (diff)
downloadrust-abecd61a230d7057aeb1dc6d82e3c8101c7d9337.tar.gz
rust-abecd61a230d7057aeb1dc6d82e3c8101c7d9337.zip
Added Result implementations for ToStr and fmt::Default
Diffstat (limited to 'src')
-rw-r--r--src/libstd/result.rs53
1 files changed, 47 insertions, 6 deletions
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<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 @@ impl<T: Clone, E> option::ToOption<T> for Result<T, E> {
 
 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 @@ impl<T, E> option::IntoOption<T> for Result<T, E> {
 
 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 @@ impl<T, E> AsResult<T, E> for Result<T, 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 @@ impl<T: Clone, E: Clone> either::ToEither<E, T> for Result<T, E> {
 
 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 @@ impl<T, E> either::IntoEither<E, T> for Result<T, E> {
 
 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 @@ impl<T, E> either::AsEither<E, T> for Result<T, E> {
     }
 }
 
+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 @@ 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<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)");
+    }
 }