diff --git a/core/lib/src/outcome.rs b/core/lib/src/outcome.rs index fa572e31..f338decc 100644 --- a/core/lib/src/outcome.rs +++ b/core/lib/src/outcome.rs @@ -484,6 +484,93 @@ impl Outcome { } } + /// Maps an `Outcome` to an `Outcome` by applying the + /// function `f` to the value of type `S` in `self` if `self` is an + /// `Outcome::Success`. + /// + /// # Examples + /// + /// ```rust + /// # use rocket::outcome::Outcome; + /// # use rocket::outcome::Outcome::*; + /// # + /// let x: Outcome = Success(10); + /// + /// let mapped = x.and_then(|v| match v { + /// 10 => Success("10"), + /// 1 => Forward(false), + /// _ => Failure("30") + /// }); + /// + /// assert_eq!(mapped, Success("10")); + /// ``` + #[inline] + pub fn and_then Outcome>(self, f: M) -> Outcome { + match self { + Success(val) => f(val), + Failure(val) => Failure(val), + Forward(val) => Forward(val), + } + } + + /// Maps an `Outcome` to an `Outcome` by applying the + /// function `f` to the value of type `E` in `self` if `self` is an + /// `Outcome::Failure`. + /// + /// # Examples + /// + /// ```rust + /// # use rocket::outcome::Outcome; + /// # use rocket::outcome::Outcome::*; + /// # + /// let x: Outcome = Failure("hi"); + /// + /// let mapped = x.failure_then(|v| match v { + /// "hi" => Failure(10), + /// "test" => Forward(false), + /// _ => Success(10) + /// }); + /// + /// assert_eq!(mapped, Failure(10)); + /// ``` + #[inline] + pub fn failure_then Outcome>(self, f: M) -> Outcome { + match self { + Success(val) => Success(val), + Failure(val) => f(val), + Forward(val) => Forward(val), + } + } + + /// Maps an `Outcome` to an `Outcome` by applying the + /// function `f` to the value of type `F` in `self` if `self` is an + /// `Outcome::Forward`. + /// + /// # Examples + /// + /// ```rust + /// # use rocket::outcome::Outcome; + /// # use rocket::outcome::Outcome::*; + /// # + /// let x: Outcome> = Forward(Some(false)); + /// + /// let mapped = x.forward_then(|v| match v { + /// Some(true) => Success(10), + /// Some(false) => Forward(20), + /// None => Failure("10") + /// }); + /// + /// assert_eq!(mapped, Forward(20)); + /// ``` + #[inline] + pub fn forward_then Outcome>(self, f: M) -> Outcome { + match self { + Success(val) => Success(val), + Failure(val) => Failure(val), + Forward(val) => f(val), + } + } + /// Converts from `Outcome` to `Outcome<&mut S, &mut E, &mut F>`. /// /// ```rust