continuwuity/src/core/utils/mod.rs
Jason Volk 7e4453620e move exponential backoff util to different submod
Signed-off-by: Jason Volk <jason@zemos.net>
2025-01-01 23:28:01 -05:00

166 lines
3 KiB
Rust

pub mod arrayvec;
pub mod bool;
pub mod bytes;
pub mod content_disposition;
pub mod debug;
pub mod defer;
pub mod future;
pub mod hash;
pub mod html;
pub mod json;
pub mod math;
pub mod mutex_map;
pub mod rand;
pub mod result;
pub mod set;
pub mod stream;
pub mod string;
pub mod sys;
mod tests;
pub mod time;
pub use ::conduwuit_macros::implement;
pub use ::ctor::{ctor, dtor};
pub use self::{
arrayvec::ArrayVecExt,
bool::BoolExt,
bytes::{increment, u64_from_bytes, u64_from_u8, u64_from_u8x8},
debug::slice_truncated as debug_slice_truncated,
future::TryExtExt as TryFutureExtExt,
hash::sha256::delimited as calculate_hash,
html::Escape as HtmlEscape,
json::{deserialize_from_str, to_canonical_object},
math::clamp,
mutex_map::{Guard as MutexMapGuard, MutexMap},
rand::{shuffle, string as random_string},
stream::{IterStream, ReadyExt, Tools as StreamTools, TryReadyExt},
string::{str_from_bytes, string_from_bytes},
sys::compute::parallelism as available_parallelism,
time::{
exponential_backoff::{continue_exponential_backoff, continue_exponential_backoff_secs},
now_millis as millis_since_unix_epoch, timepoint_ago, timepoint_from_now,
},
};
#[inline]
pub fn exchange<T>(state: &mut T, source: T) -> T { std::mem::replace(state, source) }
#[macro_export]
macro_rules! extract_variant {
($e:expr, $variant:path) => {
match $e {
| $variant(value) => Some(value),
| _ => None,
}
};
}
#[macro_export]
macro_rules! apply {
(1, $($idx:tt)+) => {
|t| (($($idx)+)(t.0),)
};
(2, $($idx:tt)+) => {
|t| (($($idx)+)(t.0), ($($idx)+)(t.1),)
};
(3, $($idx:tt)+) => {
|t| (($($idx)+)(t.0), ($($idx)+)(t.1), ($($idx)+)(t.2),)
};
(4, $($idx:tt)+) => {
|t| (($($idx)+)(t.0), ($($idx)+)(t.1), ($($idx)+)(t.2), ($($idx)+4)(t.3))
};
}
#[macro_export]
macro_rules! at {
($idx:tt) => {
|t| t.$idx
};
}
#[macro_export]
macro_rules! ref_at {
($idx:tt) => {
|ref t| &t.$idx
};
}
/// Functor for equality i.e. .is_some_and(is_equal!(2))
#[macro_export]
macro_rules! is_equal_to {
($val:ident) => {
|x| x == $val
};
($val:expr) => {
|x| x == $val
};
}
/// Functor for less i.e. .is_some_and(is_less_than!(2))
#[macro_export]
macro_rules! is_less_than {
($val:ident) => {
|x| x < $val
};
($val:expr) => {
|x| x < $val
};
}
/// Functor for equality to zero
#[macro_export]
macro_rules! is_zero {
() => {
$crate::is_matching!(0)
};
}
/// Functor for matches! i.e. .is_some_and(is_matching!('A'..='Z'))
#[macro_export]
macro_rules! is_matching {
($val:ident) => {
|x| matches!(x, $val)
};
($($val:tt)+) => {
|x| matches!(x, $($val)+)
};
}
/// Functor for !is_empty()
#[macro_export]
macro_rules! is_not_empty {
() => {
|x| !x.is_empty()
};
}
/// Functor for equality i.e. (a, b).map(is_equal!())
#[macro_export]
macro_rules! is_equal {
() => {
|a, b| a == b
};
}
/// Functor for truthy
#[macro_export]
macro_rules! is_true {
() => {
|x| !!x
};
}
/// Functor for falsy
#[macro_export]
macro_rules! is_false {
() => {
|x| !x
};
}