Add Result traits map_ref()/and_then_ref().
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
mod and_then_ref;
|
||||||
mod debug_inspect;
|
mod debug_inspect;
|
||||||
mod filter;
|
mod filter;
|
||||||
mod flat_ok;
|
mod flat_ok;
|
||||||
@@ -5,14 +6,16 @@ mod into_is_ok;
|
|||||||
mod log_debug_err;
|
mod log_debug_err;
|
||||||
mod log_err;
|
mod log_err;
|
||||||
mod map_expect;
|
mod map_expect;
|
||||||
|
mod map_ref;
|
||||||
mod not_found;
|
mod not_found;
|
||||||
mod unwrap_infallible;
|
mod unwrap_infallible;
|
||||||
mod unwrap_or_err;
|
mod unwrap_or_err;
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
debug_inspect::DebugInspect, filter::Filter, flat_ok::FlatOk, into_is_ok::IntoIsOk,
|
and_then_ref::AndThenRef, debug_inspect::DebugInspect, filter::Filter, flat_ok::FlatOk,
|
||||||
log_debug_err::LogDebugErr, log_err::LogErr, map_expect::MapExpect, not_found::NotFound,
|
into_is_ok::IntoIsOk, log_debug_err::LogDebugErr, log_err::LogErr, map_expect::MapExpect,
|
||||||
unwrap_infallible::UnwrapInfallible, unwrap_or_err::UnwrapOrErr,
|
map_ref::MapRef, not_found::NotFound, unwrap_infallible::UnwrapInfallible,
|
||||||
|
unwrap_or_err::UnwrapOrErr,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type Result<T = (), E = crate::Error> = std::result::Result<T, E>;
|
pub type Result<T = (), E = crate::Error> = std::result::Result<T, E>;
|
||||||
|
|||||||
17
src/core/utils/result/and_then_ref.rs
Normal file
17
src/core/utils/result/and_then_ref.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use super::Result;
|
||||||
|
|
||||||
|
pub trait AndThenRef<T, E> {
|
||||||
|
fn and_then_ref<U, F>(self, op: F) -> Result<U, E>
|
||||||
|
where
|
||||||
|
F: FnOnce(&T) -> Result<U, E>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, E> AndThenRef<T, E> for Result<T, E> {
|
||||||
|
#[inline]
|
||||||
|
fn and_then_ref<U, F>(self, op: F) -> Result<U, E>
|
||||||
|
where
|
||||||
|
F: FnOnce(&T) -> Result<U, E>,
|
||||||
|
{
|
||||||
|
self.and_then(|t| op(&t))
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/core/utils/result/map_ref.rs
Normal file
17
src/core/utils/result/map_ref.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use super::Result;
|
||||||
|
|
||||||
|
pub trait MapRef<T, E> {
|
||||||
|
fn map_ref<U, F>(self, op: F) -> Result<U, E>
|
||||||
|
where
|
||||||
|
F: FnOnce(&T) -> U;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, E> MapRef<T, E> for Result<T, E> {
|
||||||
|
#[inline]
|
||||||
|
fn map_ref<U, F>(self, op: F) -> Result<U, E>
|
||||||
|
where
|
||||||
|
F: FnOnce(&T) -> U,
|
||||||
|
{
|
||||||
|
self.map(|t| op(&t))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user