about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2013-09-11 12:52:17 -0700
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2013-09-12 18:54:13 -0700
commit38f97ea10313ba9a8c6f57fbf73ff8daf5376e8b (patch)
tree72966f38845b07f6f43291d06957634135d97030 /src/libstd
parente03d60e9ebf2dbc2d18ab9919f905c17b967fcde (diff)
downloadrust-38f97ea10313ba9a8c6f57fbf73ff8daf5376e8b.tar.gz
rust-38f97ea10313ba9a8c6f57fbf73ff8daf5376e8b.zip
std: Rename {Option,Result}::chain{,_err}* to {and_then,or_else}
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/io.rs6
-rw-r--r--src/libstd/iter.rs19
-rw-r--r--src/libstd/option.rs42
-rw-r--r--src/libstd/result.rs65
-rw-r--r--src/libstd/rt/io/net/ip.rs2
5 files changed, 79 insertions, 55 deletions
diff --git a/src/libstd/io.rs b/src/libstd/io.rs
index e9b704c2686..2ca36de4f49 100644
--- a/src/libstd/io.rs
+++ b/src/libstd/io.rs
@@ -1618,7 +1618,7 @@ impl<T:Writer> WriterUtil for T {
 }
 
 pub fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str> {
-    mk_file_writer(path, flags).chain(|w| Ok(w))
+    mk_file_writer(path, flags).and_then(|w| Ok(w))
 }
 
 
@@ -1779,7 +1779,7 @@ pub fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) ->
 }
 
 pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> {
-    do read_whole_file(file).chain |bytes| {
+    do read_whole_file(file).and_then |bytes| {
         if str::is_utf8(bytes) {
             Ok(str::from_utf8(bytes))
         } else {
@@ -1791,7 +1791,7 @@ pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> {
 // FIXME (#2004): implement this in a low-level way. Going through the
 // abstractions is pointless.
 pub fn read_whole_file(file: &Path) -> Result<~[u8], ~str> {
-    do file_reader(file).chain |rdr| {
+    do file_reader(file).and_then |rdr| {
         Ok(rdr.read_whole_stream())
     }
 }
diff --git a/src/libstd/iter.rs b/src/libstd/iter.rs
index 5ca827350d0..f36fb2d98bd 100644
--- a/src/libstd/iter.rs
+++ b/src/libstd/iter.rs
@@ -1474,7 +1474,7 @@ pub struct Scan<'self, A, B, T, St> {
 impl<'self, A, B, T: Iterator<A>, St> Iterator<B> for Scan<'self, A, B, T, St> {
     #[inline]
     fn next(&mut self) -> Option<B> {
-        self.iter.next().chain(|a| (self.f)(&mut self.state, a))
+        self.iter.next().and_then(|a| (self.f)(&mut self.state, a))
     }
 
     #[inline]
@@ -1494,8 +1494,7 @@ pub struct FlatMap<'self, A, T, U> {
     priv backiter: Option<U>,
 }
 
-impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for
-    FlatMap<'self, A, T, U> {
+impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for FlatMap<'self, A, T, U> {
     #[inline]
     fn next(&mut self) -> Option<B> {
         loop {
@@ -1505,7 +1504,12 @@ impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for
                 }
             }
             match self.iter.next().map_move(|x| (self.f)(x)) {
-                None => return self.backiter.chain_mut_ref(|it| it.next()),
+                None => {
+                    return match self.backiter {
+                        Some(ref mut it) => it.next(),
+                        None => None,
+                    };
+                }
                 next => self.frontiter = next,
             }
         }
@@ -1537,7 +1541,12 @@ impl<'self,
                 }
             }
             match self.iter.next_back().map_move(|x| (self.f)(x)) {
-                None => return self.frontiter.chain_mut_ref(|it| it.next_back()),
+                None => {
+                    return match self.frontiter {
+                        Some(ref mut it) => it.next_back(),
+                        None => None,
+                    };
+                }
                 next => self.backiter = next,
             }
         }
diff --git a/src/libstd/option.rs b/src/libstd/option.rs
index cd9e3980716..9b5b28d5ceb 100644
--- a/src/libstd/option.rs
+++ b/src/libstd/option.rs
@@ -141,9 +141,9 @@ impl<T> Option<T> {
     /// Returns `None` if the option is `None`, otherwise calls and returns the
     /// value of `f`.
     #[inline]
-    pub fn and_then(self, f: &fn() -> Option<T>) -> Option<T> {
+    pub fn and_then<U>(self, f: &fn(T) -> Option<U>) -> Option<U> {
         match self {
-            Some(_) => f(),
+            Some(x) => f(x),
             None => None,
         }
     }
@@ -167,36 +167,6 @@ impl<T> Option<T> {
         }
     }
 
-    /// Update an optional value by optionally running its content through a
-    /// function that returns an option.
-    #[inline]
-    pub fn chain<U>(self, f: &fn(T) -> Option<U>) -> Option<U> {
-        match self {
-            Some(t) => f(t),
-            None => None
-        }
-    }
-
-    /// Update an optional value by optionally running its content by reference
-    /// through a function that returns an option.
-    #[inline]
-    pub fn chain_ref<'a, U>(&'a self, f: &fn(x: &'a T) -> Option<U>) -> Option<U> {
-        match *self {
-            Some(ref x) => f(x),
-            None => None
-        }
-    }
-
-    /// Update an optional value by optionally running its content by mut reference
-    /// through a function that returns an option.
-    #[inline]
-    pub fn chain_mut_ref<'a, U>(&'a mut self, f: &fn(x: &'a mut T) -> Option<U>) -> Option<U> {
-        match *self {
-            Some(ref mut x) => f(x),
-            None => None
-        }
-    }
-
     /// Filters an optional value using given function.
     #[inline(always)]
     pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> {
@@ -637,12 +607,12 @@ mod tests {
     #[test]
     fn test_and_then() {
         let x: Option<int> = Some(1);
-        assert_eq!(x.and_then(|| Some(2)), Some(2));
-        assert_eq!(x.and_then(|| None), None);
+        assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
+        assert_eq!(x.and_then(|_| None::<int>), None);
 
         let x: Option<int> = None;
-        assert_eq!(x.and_then(|| Some(2)), None);
-        assert_eq!(x.and_then(|| None), None);
+        assert_eq!(x.and_then(|x| Some(x + 1)), None);
+        assert_eq!(x.and_then(|_| None::<int>), None);
     }
 
     #[test]
diff --git a/src/libstd/result.rs b/src/libstd/result.rs
index 20b65f1576d..3811f34cec4 100644
--- a/src/libstd/result.rs
+++ b/src/libstd/result.rs
@@ -173,8 +173,20 @@ impl<T, E: ToStr> Result<T, E> {
 
     /// Call a method based on a previous result
     ///
+    /// If `self` is `Ok`, then `res` it is returned. If `self` is `Err`,
+    /// then `self` is returned.
+    #[inline]
+    pub fn and(self, res: Result<T, E>) -> Result<T, E> {
+        match self {
+            Ok(_) => res,
+            Err(_) => self,
+        }
+    }
+
+    /// Call a method based on a previous result
+    ///
     /// If `self` is `Ok` then the value is extracted and passed to `op`
-    /// whereupon `op`s result is returned. if `self` is `Err` then it is
+    /// whereupon `op`s result is returned. If `self` is `Err` then it is
     /// immediately returned. This function can be used to compose the results
     /// of two functions.
     ///
@@ -184,13 +196,25 @@ impl<T, E: ToStr> Result<T, E> {
     ///         Ok(parse_bytes(buf))
     ///     };
     #[inline]
-    pub fn chain<U>(self, op: &fn(T) -> Result<U, E>) -> Result<U, E> {
+    pub fn and_then<U>(self, op: &fn(T) -> Result<U, E>) -> Result<U, E> {
         match self {
             Ok(t) => op(t),
             Err(e) => Err(e),
         }
     }
 
+    /// Call a method based on a previous result
+    ///
+    /// If `self` is `Ok`, then `self` is returned. If `self` is `Err`
+    /// then `res` is returned.
+    #[inline]
+    pub fn or(self, res: Result<T, E>) -> Result<T, E> {
+        match self {
+            Ok(_) => self,
+            Err(_) => res,
+        }
+    }
+
     /// Call a function based on a previous result
     ///
     /// If `self` is `Err` then the value is extracted and passed to `op`
@@ -198,7 +222,7 @@ impl<T, E: ToStr> Result<T, E> {
     /// immediately returned.  This function can be used to pass through a
     /// successful result while handling an error.
     #[inline]
-    pub fn chain_err<F>(self, op: &fn(E) -> Result<T, F>) -> Result<T, F> {
+    pub fn or_else<F>(self, op: &fn(E) -> Result<T, F>) -> Result<T, F> {
         match self {
             Ok(t) => Ok(t),
             Err(e) => op(e),
@@ -430,21 +454,42 @@ mod tests {
     use vec::ImmutableVector;
 
     pub fn op1() -> Result<int, ~str> { Ok(666) }
+    pub fn op2() -> Result<int, ~str> { Err(~"sadface") }
+
+    #[test]
+    pub fn test_and() {
+        assert_eq!(op1().and(Ok(667)).unwrap(), 667);
+        assert_eq!(op1().and(Err(~"bad")).unwrap_err(), ~"bad");
 
-    pub fn op2(i: int) -> Result<uint, ~str> {
-        Ok(i as uint + 1u)
+        assert_eq!(op2().and(Ok(667)).unwrap_err(), ~"sadface");
+        assert_eq!(op2().and(Err(~"bad")).unwrap_err(), ~"sadface");
     }
 
-    pub fn op3() -> Result<int, ~str> { Err(~"sadface") }
+    #[test]
+    pub fn test_and_then() {
+        assert_eq!(op1().and_then(|i| Ok::<int, ~str>(i + 1)).unwrap(), 667);
+        assert_eq!(op1().and_then(|_| Err::<int, ~str>(~"bad")).unwrap_err(), ~"bad");
+
+        assert_eq!(op2().and_then(|i| Ok::<int, ~str>(i + 1)).unwrap_err(), ~"sadface");
+        assert_eq!(op2().and_then(|_| Err::<int, ~str>(~"bad")).unwrap_err(), ~"sadface");
+    }
 
     #[test]
-    pub fn chain_success() {
-        assert_eq!(op1().chain(op2).unwrap(), 667u);
+    pub fn test_or() {
+        assert_eq!(op1().or(Ok(667)).unwrap(), 666);
+        assert_eq!(op1().or(Err(~"bad")).unwrap(), 666);
+
+        assert_eq!(op2().or(Ok(667)).unwrap(), 667);
+        assert_eq!(op2().or(Err(~"bad")).unwrap_err(), ~"bad");
     }
 
     #[test]
-    pub fn chain_failure() {
-        assert_eq!(op3().chain( op2).unwrap_err(), ~"sadface");
+    pub fn test_or_else() {
+        assert_eq!(op1().or_else(|_| Ok::<int, ~str>(667)).unwrap(), 666);
+        assert_eq!(op1().or_else(|e| Err::<int, ~str>(e + "!")).unwrap(), 666);
+
+        assert_eq!(op2().or_else(|_| Ok::<int, ~str>(667)).unwrap(), 667);
+        assert_eq!(op2().or_else(|e| Err::<int, ~str>(e + "!")).unwrap_err(), ~"sadface!");
     }
 
     #[test]
diff --git a/src/libstd/rt/io/net/ip.rs b/src/libstd/rt/io/net/ip.rs
index 956dd08ac91..041253455f0 100644
--- a/src/libstd/rt/io/net/ip.rs
+++ b/src/libstd/rt/io/net/ip.rs
@@ -177,7 +177,7 @@ impl<'self> Parser<'self> {
         }
 
         do self.read_atomically |p| {
-            p.read_char().chain(|c| parse_digit(c, radix))
+            p.read_char().and_then(|c| parse_digit(c, radix))
         }
     }