add MapExpect to Result
add DebugInspect to Result move Result typedef into unit Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
99ad404ea9
commit
2709995f84
5 changed files with 78 additions and 5 deletions
52
src/core/result/debug_inspect.rs
Normal file
52
src/core/result/debug_inspect.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use super::Result;
|
||||
|
||||
/// Inspect Result values with release-mode elision.
|
||||
pub trait DebugInspect<T, E> {
|
||||
/// Inspects an Err contained value in debug-mode. In release-mode closure F
|
||||
/// is elided.
|
||||
#[must_use]
|
||||
fn debug_inspect_err<F: FnOnce(&E)>(self, f: F) -> Self;
|
||||
|
||||
/// Inspects an Ok contained value in debug-mode. In release-mode closure F
|
||||
/// is elided.
|
||||
#[must_use]
|
||||
fn debug_inspect<F: FnOnce(&T)>(self, f: F) -> Self;
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
impl<T, E> DebugInspect<T, E> for Result<T, E> {
|
||||
#[inline]
|
||||
fn debug_inspect<F>(self, f: F) -> Self
|
||||
where
|
||||
F: FnOnce(&T),
|
||||
{
|
||||
self.inspect(f)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn debug_inspect_err<F>(self, f: F) -> Self
|
||||
where
|
||||
F: FnOnce(&E),
|
||||
{
|
||||
self.inspect_err(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
impl<T, E> DebugInspect<T, E> for Result<T, E> {
|
||||
#[inline]
|
||||
fn debug_inspect<F>(self, _: F) -> Self
|
||||
where
|
||||
F: FnOnce(&T),
|
||||
{
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn debug_inspect_err<F>(self, _: F) -> Self
|
||||
where
|
||||
F: FnOnce(&E),
|
||||
{
|
||||
self
|
||||
}
|
||||
}
|
15
src/core/result/map_expect.rs
Normal file
15
src/core/result/map_expect.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use std::fmt::Debug;
|
||||
|
||||
use super::Result;
|
||||
|
||||
pub trait MapExpect<T> {
|
||||
/// Calls expect(msg) on the mapped Result value. This is similar to
|
||||
/// map(Result::unwrap) but composes an expect call and message without
|
||||
/// requiring a closure.
|
||||
fn map_expect(self, msg: &str) -> Option<T>;
|
||||
}
|
||||
|
||||
impl<T, E: Debug> MapExpect<T> for Option<Result<T, E>> {
|
||||
#[inline]
|
||||
fn map_expect(self, msg: &str) -> Option<T> { self.map(|result| result.expect(msg)) }
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue