add FlatOk trait to Result/Option suite
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
08a2fecc0e
commit
a2e5c3d5d3
2 changed files with 36 additions and 1 deletions
|
@ -1,4 +1,5 @@
|
|||
mod debug_inspect;
|
||||
mod flat_ok;
|
||||
mod into_is_ok;
|
||||
mod log_debug_err;
|
||||
mod log_err;
|
||||
|
@ -7,7 +8,7 @@ mod not_found;
|
|||
mod unwrap_infallible;
|
||||
|
||||
pub use self::{
|
||||
debug_inspect::DebugInspect, into_is_ok::IntoIsOk, log_debug_err::LogDebugErr, log_err::LogErr,
|
||||
debug_inspect::DebugInspect, flat_ok::FlatOk, into_is_ok::IntoIsOk, log_debug_err::LogDebugErr, log_err::LogErr,
|
||||
map_expect::MapExpect, not_found::NotFound, unwrap_infallible::UnwrapInfallible,
|
||||
};
|
||||
|
||||
|
|
34
src/core/result/flat_ok.rs
Normal file
34
src/core/result/flat_ok.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use super::Result;
|
||||
|
||||
pub trait FlatOk<T> {
|
||||
/// Equivalent to .transpose().ok().flatten()
|
||||
fn flat_ok(self) -> Option<T>;
|
||||
|
||||
/// Equivalent to .transpose().ok().flatten().ok_or(...)
|
||||
fn flat_ok_or<E>(self, err: E) -> Result<T, E>;
|
||||
|
||||
/// Equivalent to .transpose().ok().flatten().ok_or_else(...)
|
||||
fn flat_ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E>;
|
||||
}
|
||||
|
||||
impl<T, E> FlatOk<T> for Option<Result<T, E>> {
|
||||
#[inline]
|
||||
fn flat_ok(self) -> Option<T> { self.transpose().ok().flatten() }
|
||||
|
||||
#[inline]
|
||||
fn flat_ok_or<Ep>(self, err: Ep) -> Result<T, Ep> { self.flat_ok().ok_or(err) }
|
||||
|
||||
#[inline]
|
||||
fn flat_ok_or_else<Ep, F: FnOnce() -> Ep>(self, err: F) -> Result<T, Ep> { self.flat_ok().ok_or_else(err) }
|
||||
}
|
||||
|
||||
impl<T, E> FlatOk<T> for Result<Option<T>, E> {
|
||||
#[inline]
|
||||
fn flat_ok(self) -> Option<T> { self.ok().flatten() }
|
||||
|
||||
#[inline]
|
||||
fn flat_ok_or<Ep>(self, err: Ep) -> Result<T, Ep> { self.flat_ok().ok_or(err) }
|
||||
|
||||
#[inline]
|
||||
fn flat_ok_or_else<Ep, F: FnOnce() -> Ep>(self, err: F) -> Result<T, Ep> { self.flat_ok().ok_or_else(err) }
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue