#[cfg(feature = "future")]
pub use self::future::TapFutureOps;
#[cfg(feature = "future")]
mod future;
#[cfg(feature = "nom3")]
pub use self::nom::TapNomOps;
#[cfg(feature = "nom3")]
mod nom;
pub trait TapBooleanOps {
fn tap_true<R, F: FnOnce() -> R>(self, f: F) -> Self;
fn tap_false<R, F: FnOnce() -> R>(self, f: F) -> Self;
}
impl TapBooleanOps for bool {
fn tap_true<R, F: FnOnce() -> R>(self, f: F) -> Self {
if self {
let _ = f();
}
self
}
fn tap_false<R, F: FnOnce() -> R>(self, f: F) -> Self {
if !self {
let _ = f();
}
self
}
}
pub trait TapResultOps<T, E> {
fn tap_ok<R, F: FnOnce(&mut T) -> R>(self, f: F) -> Self;
fn tap_err<R, F: FnOnce(&mut E) -> R>(self, f: F) -> Self;
}
impl<T, E> TapResultOps<T, E> for Result<T, E> {
fn tap_ok<R, F: FnOnce(&mut T) -> R>(mut self, f: F) -> Self {
if let Ok(mut val) = self.as_mut() {
let _ = f(&mut val);
}
self
}
fn tap_err<R, F: FnOnce(&mut E) -> R>(mut self, f: F) -> Self {
if let Err(mut val) = self.as_mut() {
let _ = f(&mut val);
}
self
}
}
pub trait TapOptionOps<T> {
fn tap_some<R, F: FnOnce(&mut T) -> R>(self, f: F) -> Self;
fn tap_none<R, F: FnOnce() -> R>(self, f: F) -> Self;
}
impl<T> TapOptionOps<T> for Option<T> {
fn tap_some<R, F: FnOnce(&mut T) -> R>(mut self, f: F) -> Self {
if let Some(val) = self.as_mut() {
let _ = f(val);
}
self
}
fn tap_none<R, F: FnOnce() -> R>(self, f: F) -> Self {
if self.is_none() {
let _ = f();
}
self
}
}
pub trait TapOps: Sized {
fn tap<R, F>(self, f: F) -> Self
where F: FnOnce(&mut Self) -> R;
}
impl<T> TapOps for T where T: Sized {
fn tap<R, F>(mut self, f: F) -> Self
where F: FnOnce(&mut Self) -> R
{
let _ = f(&mut self);
self
}
}