tuwunel_core/utils/result/unwrap_or_err.rs
1use std::convert::identity;
2
3use super::Result;
4
5/// Returns the contained value from either variant of a `Result<T, T>`.
6///
7/// Unlike `unwrap_or_default`, the `Err` branch retains its value instead of
8/// constructing `T::default()`.
9pub trait UnwrapOrErr<T> {
10 /// Returns the value carried by either result variant.
11 ///
12 /// Both variants contain the same type, so neither branch needs conversion.
13 /// The result is consumed without constructing a fallback value.
14 fn unwrap_or_err(self) -> T;
15}
16
17impl<T> UnwrapOrErr<T> for Result<T, T> {
18 #[inline]
19 fn unwrap_or_err(self) -> T { self.unwrap_or_else(identity::<T>) }
20}