Skip to main content

tuwunel_core/utils/
mod.rs

1pub mod arrayvec;
2pub mod bool;
3pub mod bytes;
4pub mod content_disposition;
5pub mod debug;
6pub mod defer;
7pub mod future;
8pub mod hash;
9pub mod html;
10pub mod json;
11pub mod math;
12pub mod mutex_map;
13pub mod option;
14pub mod rand;
15pub mod result;
16pub mod set;
17pub mod stream;
18pub mod string;
19pub mod sys;
20#[cfg(test)]
21mod tests;
22pub mod time;
23pub mod two_phase_counter;
24pub mod unhandled;
25
26pub use ::ctor::ctor;
27pub use ::dtor::dtor;
28pub use ::tuwunel_macros::implement;
29
30pub use self::{
31	arrayvec::ArrayVecExt,
32	bool::BoolExt,
33	bytes::{increment, u64_from_bytes, u64_from_u8},
34	debug::slice_truncated as debug_slice_truncated,
35	future::{BoolExt as FutureBoolExt, OptionStream, TryExtExt as TryFutureExtExt},
36	hash::sha256::delimited as calculate_hash,
37	json::{deserialize_from_str, to_canonical_object},
38	mutex_map::{Guard as MutexMapGuard, MutexMap},
39	option::OptionExt,
40	rand::{shuffle, string as random_string, string_from as random_string_from},
41	stream::{IterStream, ReadyExt, Tools as StreamTools, TryReadyExt},
42	string::{str_from_bytes, string_from_bytes},
43	sys::compute::available_parallelism,
44	time::{
45		exponential_backoff::{
46			continue_exponential_backoff, continue_exponential_backoff_secs,
47			exponential_backoff_streak_cap,
48		},
49		now_millis as millis_since_unix_epoch, timepoint_ago, timepoint_from_now,
50		timepoint_has_passed,
51	},
52};
53
54pub const fn assert_send<T: Send>() {}
55pub const fn assert_sync<T: Sync>() {}
56pub const fn assert_dst<T: ?Sized>() {}
57pub const fn assert_sized<T: Sized>() {}
58pub const fn assert_unpin<T: Unpin>() {}
59pub const fn assert_unwind_safe<T: std::panic::UnwindSafe>() {}
60pub const fn assert_ref_unwind_safe<T: std::panic::RefUnwindSafe>() {}
61
62#[macro_export]
63macro_rules! extract_variant {
64	( $e:expr_2021, $( $variant:path )|* ) => {
65		match $e {
66			$( $variant(value) => Some(value), )*
67			_ => None,
68		}
69	};
70}
71
72#[macro_export]
73macro_rules! extract {
74	($e:expr_2021, $out:ident in $variant:pat) => {
75		match $e {
76			| $variant => Some($out),
77			| _ => None,
78		}
79	};
80}
81
82/// Functor for !is_empty()
83#[macro_export]
84macro_rules! is_not_empty {
85	() => {
86		|x| !x.is_empty()
87	};
88}
89
90#[macro_export]
91macro_rules! apply {
92	(1, $($idx:tt)+) => {
93		|t| (($($idx)+)(t.0),)
94	};
95
96	(2, $($idx:tt)+) => {
97		|t| (($($idx)+)(t.0), ($($idx)+)(t.1),)
98	};
99
100	(3, $($idx:tt)+) => {
101		|t| (($($idx)+)(t.0), ($($idx)+)(t.1), ($($idx)+)(t.2),)
102	};
103
104	(4, $($idx:tt)+) => {
105		|t| (($($idx)+)(t.0), ($($idx)+)(t.1), ($($idx)+)(t.2), ($($idx)+)(t.3),)
106	};
107
108	(5, $($idx:tt)+) => {
109		|t| (($($idx)+)(t.0), ($($idx)+)(t.1), ($($idx)+)(t.2), ($($idx)+)(t.3), ($($idx)+)(t.4),)
110	};
111}
112
113#[macro_export]
114macro_rules! pair_of {
115	($decl:ty) => {
116		($decl, $decl)
117	};
118
119	($init:expr_2021) => {
120		($init, $init)
121	};
122}
123
124/// Functor for truthy
125#[macro_export]
126macro_rules! is_true {
127	() => {
128		|x| !!x
129	};
130}
131
132/// Functor for falsy
133#[macro_export]
134macro_rules! is_false {
135	() => {
136		|x| !x
137	};
138}
139
140/// Functor for equality to non-zero
141#[macro_export]
142macro_rules! is_nonzero {
143	() => {
144		|x| x != 0
145	};
146}
147
148/// Functor for equality to zero
149#[macro_export]
150macro_rules! is_zero {
151	() => {
152		$crate::is_matching!(0)
153	};
154}
155
156/// Functor for equality i.e. .is_some_and(is_equal_to!(2))
157#[macro_export]
158macro_rules! is_equal_to {
159	($val:ident) => {
160		|x| x == $val
161	};
162
163	($val:expr_2021) => {
164		|x| x == $val
165	};
166}
167
168/// Functor for inequality i.e. .is_some_and(is_not_equal_to!(2))
169#[macro_export]
170macro_rules! is_not_equal_to {
171	($val:ident) => {
172		|x| x != $val
173	};
174
175	($val:expr_2021) => {
176		|x| x != $val
177	};
178}
179
180/// Functor for less i.e. .is_some_and(is_less_than!(2))
181#[macro_export]
182macro_rules! is_less_than {
183	($val:ident) => {
184		|x| x < $val
185	};
186
187	($val:expr_2021) => {
188		|x| x < $val
189	};
190}
191
192/// Functor for matches! i.e. .is_some_and(is_matching!('A'..='Z'))
193#[macro_export]
194macro_rules! is_matching {
195	($val:ident) => {
196		|x| matches!(x, $val)
197	};
198
199	($($val:tt)+) => {
200		|x| matches!(x, $($val)+)
201	};
202}
203
204/// Functor for equality i.e. (a, b).map(is_equal!())
205#[macro_export]
206macro_rules! is_equal {
207	() => {
208		|a, b| a == b
209	};
210}
211
212/// Functor for |x| *x.$i
213#[macro_export]
214macro_rules! deref_at {
215	($idx:tt) => {
216		|t| *t.$idx
217	};
218}
219
220/// Functor for |ref x| x.$i
221#[macro_export]
222macro_rules! ref_at {
223	($idx:tt) => {
224		|ref t| &t.$idx
225	};
226}
227
228/// Functor for |&x| x.$i
229#[macro_export]
230macro_rules! val_at {
231	($idx:tt) => {
232		|&t| t.$idx
233	};
234}
235
236/// Functor for |x| x.$i
237#[macro_export]
238macro_rules! at {
239	($idx:tt) => {
240		|t| t.$idx
241	};
242}