add unwrap_or_err to result
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
08a4e931a0
commit
9790a6edc9
2 changed files with 17 additions and 0 deletions
|
@ -7,10 +7,12 @@ mod log_err;
|
|||
mod map_expect;
|
||||
mod not_found;
|
||||
mod unwrap_infallible;
|
||||
mod unwrap_or_err;
|
||||
|
||||
pub use self::{
|
||||
debug_inspect::DebugInspect, filter::Filter, flat_ok::FlatOk, into_is_ok::IntoIsOk, log_debug_err::LogDebugErr,
|
||||
log_err::LogErr, map_expect::MapExpect, not_found::NotFound, unwrap_infallible::UnwrapInfallible,
|
||||
unwrap_or_err::UnwrapOrErr,
|
||||
};
|
||||
|
||||
pub type Result<T = (), E = crate::Error> = std::result::Result<T, E>;
|
||||
|
|
15
src/core/utils/result/unwrap_or_err.rs
Normal file
15
src/core/utils/result/unwrap_or_err.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use std::convert::identity;
|
||||
|
||||
use super::Result;
|
||||
|
||||
/// Returns the Ok value or the Err value. Available when the Ok and Err types
|
||||
/// are the same. This is a way to default the result using the specific Err
|
||||
/// value rather than unwrap_or_default() using Ok's default.
|
||||
pub trait UnwrapOrErr<T> {
|
||||
fn unwrap_or_err(self) -> T;
|
||||
}
|
||||
|
||||
impl<T> UnwrapOrErr<T> for Result<T, T> {
|
||||
#[inline]
|
||||
fn unwrap_or_err(self) -> T { self.unwrap_or_else(identity::<T>) }
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue