Skip to content

Commit 07dc8d6

Browse files
committed
Result::or : avoid over-specializing the type
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.
1 parent 880fb89 commit 07dc8d6

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/libcore/result.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ impl<T, E> Result<T, E> {
641641
/// ```
642642
#[inline]
643643
#[stable(feature = "rust1", since = "1.0.0")]
644-
pub fn or(self, res: Result<T, E>) -> Result<T, E> {
644+
pub fn or<F>(self, res: Result<T, F>) -> Result<T, F> {
645645
match self {
646-
Ok(_) => self,
646+
Ok(v) => Ok(v),
647647
Err(_) => res,
648648
}
649649
}

src/libcoretest/result.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ pub fn test_and_then() {
3636

3737
#[test]
3838
pub fn test_or() {
39-
assert_eq!(op1().or(Ok(667)).unwrap(), 666);
39+
assert_eq!(op1().or(Ok::<_, &'static str>(667)).unwrap(), 666);
4040
assert_eq!(op1().or(Err("bad")).unwrap(), 666);
4141

42-
assert_eq!(op2().or(Ok(667)).unwrap(), 667);
42+
assert_eq!(op2().or(Ok::<_, &'static str>(667)).unwrap(), 667);
4343
assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
4444
}
4545

0 commit comments

Comments
 (0)