use core::{
borrow::{Borrow, BorrowMut},
ops::{Deref, DerefMut},
};
pub trait Pipe<R> {
#[inline(always)]
fn pipe(self, f: impl FnOnce(Self) -> R) -> R
where
Self: Sized,
{
f(self)
}
#[inline(always)]
fn pipe_ref<'a>(&'a self, f: impl FnOnce(&'a Self) -> R) -> R {
f(self)
}
#[inline(always)]
fn pipe_mut<'a>(&'a mut self, f: impl FnOnce(&'a mut Self) -> R) -> R {
f(self)
}
#[inline(always)]
fn pipe_borrow<'a, B>(&'a self, f: impl FnOnce(&'a B) -> R) -> R
where
Self: Borrow<B>,
B: 'a + ?Sized,
{
f(Borrow::<B>::borrow(self))
}
#[inline(always)]
fn pipe_borrow_mut<'a, B>(&'a mut self, f: impl FnOnce(&'a mut B) -> R) -> R
where
Self: BorrowMut<B>,
B: 'a + ?Sized,
{
f(BorrowMut::<B>::borrow_mut(self))
}
#[inline(always)]
fn pipe_as_ref<'a, U>(&'a self, f: impl FnOnce(&'a U) -> R) -> R
where
Self: AsRef<U>,
U: 'a + ?Sized,
{
f(AsRef::<U>::as_ref(self))
}
#[inline(always)]
fn pipe_as_mut<'a, U>(&'a mut self, f: impl FnOnce(&'a mut U) -> R) -> R
where
Self: AsMut<U>,
U: 'a + ?Sized,
{
f(AsMut::<U>::as_mut(self))
}
#[inline(always)]
fn pipe_deref<'a, T>(&'a self, f: impl FnOnce(&'a T) -> R) -> R
where
Self: Deref<Target = T>,
T: 'a + ?Sized,
{
f(Deref::deref(self))
}
#[inline(always)]
fn pipe_deref_mut<'a, T>(&'a mut self, f: impl FnOnce(&'a mut T) -> R) -> R
where
Self: DerefMut + Deref<Target = T>,
T: 'a + ?Sized,
{
f(DerefMut::deref_mut(self))
}
}
impl<T, R> Pipe<R> for T where T: ?Sized {}