use crate::no_std::{
functions::ext::fn_once_ext::FnOnceExt,
pipelines::{pipe::Pipe, tap::Tap},
};
use minstant::Instant;
use std::{
fmt::{Debug, Display},
prelude::v1::*,
sync::{Arc, Mutex, RwLock},
time::Duration,
};
pub trait StdAnyExt1<R>: Sized {
fn ext_measure_time(self, f: impl FnOnce(Self) -> R) -> Duration {
Instant::now().tap(|_| f(self)).elapsed()
}
fn ext_measure_time_with_value(self, f: impl FnOnce(Self) -> R) -> (Duration, R) {
Instant::now()
.pipe(|t| (f(self), t.elapsed()))
.pipe(|(v, e)| (e, v))
}
fn ext_measure_time_with_self(self, f: impl FnOnce(&Self) -> R) -> (Duration, Self) {
Instant::now()
.tap(|_| f(&self))
.pipe(|t| (t.elapsed(), self))
}
fn ext_measure_time_with_mut_self(
mut self,
f: impl FnOnce(&mut Self) -> R,
) -> (Duration, Self) {
Instant::now()
.tap(|_| f(&mut self))
.pipe(|t| (t.elapsed(), self))
}
}
impl<T, R> StdAnyExt1<R> for T {}
pub trait StdAnyExt: Sized {
fn dbg(self) -> Self
where
Self: Debug,
{
self.tap(|s| println!("{s:#?}"))
}
fn sout(self) -> Self
where
Self: Debug,
{
self.tap(|s| println!("{s:?}"))
}
fn echo(self) -> Self
where
Self: Display,
{
self.tap(|s| println!("{s}"))
}
fn into_mutex(self) -> Mutex<Self> {
self.pipe(Mutex::new)
}
fn into_arc_mutex(self) -> Arc<Mutex<Self>> {
Arc::new.compose(Mutex::new)(self)
}
fn into_rwlock(self) -> RwLock<Self> {
self.pipe(RwLock::new)
}
fn into_arc_rwlock(self) -> Arc<RwLock<Self>> {
Arc::new.compose(RwLock::new)(self)
}
}
impl<T> StdAnyExt for T {}