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