pub trait Peep: Sized {
fn peep<F, R>(self, run: F) -> Self
where
F: FnOnce(&Self) -> R,
R: Sized,
{
run(&self);
self
}
#[allow(unused_variables)]
fn peep_dbg<F, R>(self, run: F) -> Self
where
F: FnOnce(&Self) -> R,
R: Sized,
{
#[cfg(debug_assertions)]
return self.peep(run);
#[cfg(not(debug_assertions))]
return self;
}
fn peep_mut<F, R>(mut self, run: F) -> Self
where
F: FnOnce(&mut Self) -> R,
R: Sized,
{
run(&mut self);
self
}
#[allow(unused_variables)]
fn peep_mut_dbg<F, R>(self, run: F) -> Self
where
F: FnOnce(&mut Self) -> R,
R: Sized,
{
#[cfg(debug_assertions)]
return self.peep_mut(run);
#[cfg(not(debug_assertions))]
return self;
}
}
impl<T: Sized> Peep for T {}
pub trait PeekOption<T: Sized>: Sized {
fn peep_some<F, R>(self, run: F) -> Self
where
F: FnOnce(&T) -> R,
R: Sized;
#[allow(unused_variables)]
fn peep_some_dbg<F, R>(self, run: F) -> Self
where
F: FnOnce(&T) -> R,
R: Sized,
{
#[cfg(debug_assertions)]
return self.peep_some(run);
#[cfg(not(debug_assertions))]
return self;
}
fn peep_some_mut<F, R>(self, run: F) -> Self
where
F: FnOnce(&mut T) -> R,
R: Sized;
#[allow(unused_variables)]
fn peep_some_mut_dbg<F, R>(self, run: F) -> Self
where
F: FnOnce(&mut T) -> R,
R: Sized,
{
#[cfg(debug_assertions)]
return self.peep_some_mut(run);
#[cfg(not(debug_assertions))]
return self;
}
fn peep_none<F, R>(self, run: F) -> Self
where
F: FnOnce() -> R,
R: Sized;
#[allow(unused_variables)]
fn peep_none_dbg<F, R>(self, run: F) -> Self
where
F: FnOnce() -> R,
R: Sized,
{
#[cfg(debug_assertions)]
return self.peep_none(run);
#[cfg(not(debug_assertions))]
return self;
}
}
impl<T: Sized> PeekOption<T> for Option<T> {
fn peep_some<F, R>(self, run: F) -> Self
where
F: FnOnce(&T) -> R,
R: Sized,
{
if let Some(inner) = self.as_ref() {
run(inner);
}
self
}
fn peep_some_mut<F, R>(mut self, run: F) -> Self
where
F: FnOnce(&mut T) -> R,
R: Sized,
{
if let Some(inner) = self.as_mut() {
run(inner);
}
self
}
fn peep_none<F, R>(self, run: F) -> Self
where
F: FnOnce() -> R,
R: Sized,
{
if self.is_none() {
run();
}
self
}
}
pub trait PeepResult<T: Sized, E: Sized>: Sized {
fn peek_ok<F, R>(self, run: F) -> Self
where
F: FnOnce(&T) -> R,
R: Sized;
#[allow(unused_variables)]
fn peek_ok_dbg<F, R>(self, run: F) -> Self
where
F: FnOnce(&T) -> R,
R: Sized,
{
#[cfg(debug_assertions)]
return self.peek_ok(run);
#[cfg(not(debug_assertions))]
return self;
}
fn peek_ok_mut<F, R>(self, run: F) -> Self
where
F: FnOnce(&mut T) -> R,
R: Sized;
#[allow(unused_variables)]
fn peek_ok_mut_dbg<F, R>(self, run: F) -> Self
where
F: FnOnce(&mut T) -> R,
R: Sized,
{
#[cfg(debug_assertions)]
return self.peek_ok_mut(run);
#[cfg(not(debug_assertions))]
return self;
}
fn peek_err<F, R>(self, run: F) -> Self
where
F: FnOnce(&E) -> R,
R: Sized;
#[allow(unused_variables)]
fn peek_err_dbg<F, R>(self, run: F) -> Self
where
F: FnOnce(&E) -> R,
R: Sized,
{
#[cfg(debug_assertions)]
return self.peek_err(run);
#[cfg(not(debug_assertions))]
return self;
}
fn peek_err_mut<F, R>(self, run: F) -> Self
where
F: FnOnce(&mut E) -> R,
R: Sized;
#[allow(unused_variables)]
fn peek_err_mut_dbg<F, R>(self, run: F) -> Self
where
F: FnOnce(&mut E) -> R,
R: Sized,
{
#[cfg(debug_assertions)]
return self.peek_err_mut(run);
#[cfg(not(debug_assertions))]
return self;
}
}
impl<T: Sized, E: Sized> PeepResult<T, E> for Result<T, E> {
fn peek_ok<F, R>(self, run: F) -> Self
where
F: FnOnce(&T) -> R,
R: Sized,
{
if let Ok(inner) = self.as_ref() {
run(inner);
}
self
}
fn peek_ok_mut<F, R>(mut self, run: F) -> Self
where
F: FnOnce(&mut T) -> R,
R: Sized,
{
if let Ok(inner) = self.as_mut() {
run(inner);
}
self
}
fn peek_err<F, R>(self, run: F) -> Self
where
F: FnOnce(&E) -> R,
R: Sized,
{
if let Err(inner) = self.as_ref() {
run(inner);
}
self
}
fn peek_err_mut<F, R>(mut self, run: F) -> Self
where
F: FnOnce(&mut E) -> R,
R: Sized,
{
if let Err(inner) = self.as_mut() {
run(inner);
}
self
}
}