]> git.lizzy.rs Git - rust.git/commitdiff
Result::or : avoid over-specializing the type
authorCody P Schafer <dev@codyps.com>
Wed, 25 Feb 2015 18:37:22 +0000 (13:37 -0500)
committerCody P Schafer <dev@codyps.com>
Wed, 25 Feb 2015 22:38:28 +0000 (17:38 -0500)
Changes .or() so that it can return a Result with a different E type
than the one it is called on.

Essentially:

    fn or(self, res: Result<T, E>) -> Result<T, E>

becomes

    fn or<F>(self, res: Result<T, F>) -> Result<T, F>

This brings `or` in line with the existing `and` and `or_else` member
types.

This is a
[breaking-change]
Due to some code needing additional type annotations.

src/libcore/result.rs
src/libcoretest/result.rs

index 23e936a75d7097e04f972a3da25796a285090711..ec0c19d36e4edb79352027b5ef9bd7ec68f69d3b 100644 (file)
@@ -641,9 +641,9 @@ pub fn and_then<U, F: FnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E> {
     /// ```
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn or(self, res: Result<T, E>) -> Result<T, E> {
+    pub fn or<F>(self, res: Result<T, F>) -> Result<T, F> {
         match self {
-            Ok(_) => self,
+            Ok(v) => Ok(v),
             Err(_) => res,
         }
     }
index ab7b5101e726a59f652cad6367e09f8b6c151056..10cc3ad64242720e59c2d5e9682a09d8f6aab45d 100644 (file)
@@ -36,10 +36,10 @@ pub fn test_and_then() {
 
 #[test]
 pub fn test_or() {
-    assert_eq!(op1().or(Ok(667)).unwrap(), 666);
+    assert_eq!(op1().or(Ok::<_, &'static str>(667)).unwrap(), 666);
     assert_eq!(op1().or(Err("bad")).unwrap(), 666);
 
-    assert_eq!(op2().or(Ok(667)).unwrap(), 667);
+    assert_eq!(op2().or(Ok::<_, &'static str>(667)).unwrap(), 667);
     assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
 }