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:
Jason Volk 2024-09-01 01:53:22 +00:00 committed by strawberry
parent 99ad404ea9
commit 2709995f84
5 changed files with 78 additions and 5 deletions

View File

@ -1,10 +1,10 @@
use std::{any::Any, panic}; use std::{any::Any, panic};
/// Export debug proc_macros // Export debug proc_macros
pub use conduit_macros::recursion_depth; pub use conduit_macros::recursion_depth;
/// Export all of the ancillary tools from here as well. // Export all of the ancillary tools from here as well.
pub use crate::utils::debug::*; pub use crate::{result::DebugInspect, utils::debug::*};
/// Log event at given level in debug-mode (when debug-assertions are enabled). /// Log event at given level in debug-mode (when debug-assertions are enabled).
/// In release-mode it becomes DEBUG level, and possibly subject to elision. /// In release-mode it becomes DEBUG level, and possibly subject to elision.

View File

@ -7,6 +7,7 @@ pub mod log;
pub mod metrics; pub mod metrics;
pub mod mods; pub mod mods;
pub mod pdu; pub mod pdu;
pub mod result;
pub mod server; pub mod server;
pub mod utils; pub mod utils;
@ -15,13 +16,12 @@ pub use config::Config;
pub use error::Error; pub use error::Error;
pub use info::{rustc_flags_capture, version, version::version}; pub use info::{rustc_flags_capture, version, version::version};
pub use pdu::{PduBuilder, PduCount, PduEvent}; pub use pdu::{PduBuilder, PduCount, PduEvent};
pub use result::Result;
pub use server::Server; pub use server::Server;
pub use utils::{ctor, dtor, implement}; pub use utils::{ctor, dtor, implement};
pub use crate as conduit_core; pub use crate as conduit_core;
pub type Result<T, E = Error> = std::result::Result<T, E>;
rustc_flags_capture! {} rustc_flags_capture! {}
#[cfg(not(conduit_mods))] #[cfg(not(conduit_mods))]

6
src/core/result.rs Normal file
View File

@ -0,0 +1,6 @@
mod debug_inspect;
mod map_expect;
pub use self::{debug_inspect::DebugInspect, map_expect::MapExpect};
pub type Result<T = (), E = crate::Error> = std::result::Result<T, E>;

View 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
}
}

View 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)) }
}