]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #47193 - cramertj:result-opts, r=TimNN
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Sat, 20 Jan 2018 21:32:42 +0000 (22:32 +0100)
committerGitHub <noreply@github.com>
Sat, 20 Jan 2018 21:32:42 +0000 (22:32 +0100)
Add transpose conversions for nested Option and Result

These impls are useful when working with combinator
methods that expect an option or a result, but you
have a `Result<Option<T>, E>` instead of an `Option<Result<T, E>>`
or vice versa.

1  2 
src/libcore/option.rs

diff --combined src/libcore/option.rs
index 15181dab8531cb9330fff8574e5cc9c112657708,76cea58703847fe3819e0df74abcbc87079829f2..b8fe28d0f0d71b6fa7a8c07084464754acb979d2
@@@ -407,7 -407,9 +407,7 @@@ impl<T> Option<T> 
      }
  
      /// Applies a function to the contained value (if any),
 -    /// or returns a [`default`][] (if not).
 -    ///
 -    /// [`default`]: ../default/trait.Default.html#tymethod.default
 +    /// or returns the provided default (if not).
      ///
      /// # Examples
      ///
      }
  
      /// Applies a function to the contained value (if any),
 -    /// or computes a [`default`][] (if not).
 -    ///
 -    /// [`default`]: ../default/trait.Default.html#tymethod.default
 +    /// or computes a default (if not).
      ///
      /// # Examples
      ///
@@@ -846,7 -850,7 +846,7 @@@ impl<T: Default> Option<T> 
      /// Returns the contained value or a default
      ///
      /// Consumes the `self` argument then, if [`Some`], returns the contained
 -    /// value, otherwise if [`None`], returns the default value for that
 +    /// value, otherwise if [`None`], returns the [default value] for that
      /// type.
      ///
      /// # Examples
      ///
      /// [`Some`]: #variant.Some
      /// [`None`]: #variant.None
 +    /// [default value]: ../default/trait.Default.html#tymethod.default
      /// [`parse`]: ../../std/primitive.str.html#method.parse
      /// [`FromStr`]: ../../std/str/trait.FromStr.html
      #[inline]
      }
  }
  
+ impl<T, E> Option<Result<T, E>> {
+     /// Transposes an `Option` of a `Result` into a `Result` of an `Option`.
+     ///
+     /// `None` will be mapped to `Ok(None)`.
+     /// `Some(Ok(_))` and `Some(Err(_))` will be mapped to `Ok(Some(_))` and `Err(_)`.
+     ///
+     /// # Examples
+     ///
+     /// ```
+     /// #![feature(transpose_result)]
+     ///
+     /// #[derive(Debug, Eq, PartialEq)]
+     /// struct SomeErr;
+     ///
+     /// let x: Result<Option<i32>, SomeErr> = Ok(Some(5));
+     /// let y: Option<Result<i32, SomeErr>> = Some(Ok(5));
+     /// assert_eq!(x, y.transpose());
+     /// ```
+     #[inline]
+     #[unstable(feature = "transpose_result", issue = "47338")]
+     pub fn transpose(self) -> Result<Option<T>, E> {
+         match self {
+             Some(Ok(x)) => Ok(Some(x)),
+             Some(Err(e)) => Err(e),
+             None => Ok(None),
+         }
+     }
+ }
  // This is a separate function to reduce the code size of .expect() itself.
  #[inline(never)]
  #[cold]