Skip to main content

grafix_toolbox/kit/policies/
func.rs

1#[macro_use]
2mod log;
3
4#[cfg(feature = "rng")]
5pub mod rand;
6#[cfg(not(feature = "rng"))]
7pub mod rand {}
8
9pub mod chksum;
10pub mod faster;
11pub mod file;
12pub mod logger;
13pub mod n_iter;
14pub mod range;
15pub mod result;
16pub mod serde;
17pub mod slicing;
18pub mod vec;
19
20pub mod ext {
21	pub trait Pipe: Sized {
22		#[must_use]
23		#[inline(always)]
24		#[allow(async_fn_in_trait)]
25		async fn tap_async(mut self, func: impl AsyncFnOnce(&mut Self)) -> Self {
26			func(&mut self).await;
27			self
28		}
29		#[must_use]
30		#[inline(always)]
31		fn tap(mut self, func: impl FnOnce(&mut Self)) -> Self {
32			func(&mut self);
33			self
34		}
35		#[inline(always)]
36		fn pipe<R: Sized>(self, func: impl FnOnce(Self) -> R) -> R {
37			func(self)
38		}
39		#[inline(always)]
40		fn pipe_as<'a, T: 'a + ?Sized, R: 'a + Sized>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
41		where
42			Self: std::ops::Deref<Target = T>,
43		{
44			func(std::ops::Deref::deref(self))
45		}
46	}
47	impl<T: Sized> Pipe for T {}
48
49	pub trait OrAssignment: Sized {
50		fn or_def(self, filter: bool) -> Self;
51		fn or_val(self, filter: bool, f: impl FnOnce() -> Self) -> Self; // TODO impl val/make for Sized + !&
52		fn or_map(self, filter: impl FnOnce(&Self) -> bool, f: impl FnOnce(Self) -> Self) -> Self;
53	}
54	impl<S: Default> OrAssignment for S {
55		#[inline(always)]
56		fn or_def(self, filter: bool) -> Self {
57			if filter {
58				return self;
59			}
60			Self::default()
61		}
62		#[inline(always)]
63		fn or_val(self, filter: bool, f: impl FnOnce() -> Self) -> Self {
64			if filter {
65				return self;
66			}
67			f()
68		}
69		#[inline(always)]
70		fn or_map(self, filter: impl FnOnce(&Self) -> bool, f: impl FnOnce(Self) -> Self) -> Self {
71			if filter(&self) {
72				return self;
73			}
74			f(self)
75		}
76	}
77}