diff --git a/core/lib/src/outcome.rs b/core/lib/src/outcome.rs index 3aa7b8d7..8a150306 100644 --- a/core/lib/src/outcome.rs +++ b/core/lib/src/outcome.rs @@ -165,10 +165,11 @@ impl Outcome { /// assert_eq!(x.unwrap(), 10); /// ``` #[inline] + #[track_caller] pub fn unwrap(self) -> S { match self { Success(val) => val, - _ => panic!("Expected a successful outcome!") + _ => panic!("unwrapped a non-successful outcome") } } @@ -188,10 +189,11 @@ impl Outcome { /// assert_eq!(x.expect("success value"), 10); /// ``` #[inline] + #[track_caller] pub fn expect(self, message: &str) -> S { match self { Success(val) => val, - _ => panic!("Outcome::expect() failed: {}", message) + _ => panic!("unwrapped a non-successful outcome: {}", message) } } @@ -417,6 +419,28 @@ impl Outcome { } } + /// Converts from `Outcome` to `Outcome<&mut S, &mut E, &mut F>`. + /// + /// ```rust + /// # use rocket::outcome::Outcome; + /// # use rocket::outcome::Outcome::*; + /// # + /// let mut x: Outcome = Success(10); + /// if let Success(val) = x.as_mut() { + /// *val = 20; + /// } + /// + /// assert_eq!(x.unwrap(), 20); + /// ``` + #[inline] + pub fn as_mut(&mut self) -> Outcome<&mut S, &mut E, &mut F> { + match *self { + Success(ref mut val) => Success(val), + Failure(ref mut val) => Failure(val), + Forward(ref mut val) => Forward(val), + } + } + /// Maps the `Success` value using `f`. 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`. @@ -483,10 +507,10 @@ impl Outcome { } } - /// Maps the `Success` value using `f()`, returning the `Outcome` from `f()` - /// or the original `self` if `self` is not `Success`. 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`. + /// Converts from `Outcome` to `Outcome` using `f` to map + /// `Success(S)` to `Success(T)`. + /// + /// If `self` is not `Success`, `self` is returned. /// /// # Examples /// @@ -513,11 +537,10 @@ impl Outcome { } } - /// Maps the `Failure` value using `f()`, returning the `Outcome` from `f()` - /// or the original `self` if `self` is not `Failure`. 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`. + /// Converts from `Outcome` to `Outcome` using `f` to map + /// `Failure(E)` to `Failure(T)`. + /// + /// If `self` is not `Failure`, `self` is returned. /// /// # Examples /// @@ -544,10 +567,10 @@ impl Outcome { } } - /// Maps the `Forward` value using `f()`, returning the `Outcome` from `f()` - /// or the original `self` if `self` is not `Forward`. 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`. + /// Converts from `Outcome` to `Outcome` using `f` to map + /// `Forward(F)` to `Forward(T)`. + /// + /// If `self` is not `Forward`, `self` is returned. /// /// # Examples /// @@ -574,27 +597,6 @@ impl Outcome { } } - /// Converts from `Outcome` to `Outcome<&mut S, &mut E, &mut F>`. - /// - /// ```rust - /// # use rocket::outcome::Outcome; - /// # use rocket::outcome::Outcome::*; - /// # - /// let mut x: Outcome = Success(10); - /// if let Success(val) = x.as_mut() { - /// *val = 20; - /// } - /// - /// assert_eq!(x.unwrap(), 20); - /// ``` - #[inline] - pub fn as_mut(&mut self) -> Outcome<&mut S, &mut E, &mut F> { - match *self { - Success(ref mut val) => Success(val), - Failure(ref mut val) => Failure(val), - Forward(ref mut val) => Forward(val), - } - } #[inline] fn formatting(&self) -> (Color, &'static str) { @@ -605,6 +607,7 @@ impl Outcome { } } } + impl<'a, S: Send + 'a, E: Send + 'a, F: Send + 'a> Outcome { /// Pins a future that resolves to `self`, returning a /// [`BoxFuture`](crate::futures::future::BoxFuture) that resolves to