Files
continuwuity/src/core/utils/mod.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

175 lines
3.0 KiB
Rust
Raw Normal View History

2024-10-25 19:53:08 +00:00
pub mod arrayvec;
2024-09-30 06:46:54 +00:00
pub mod bool;
2024-07-03 00:54:36 +00:00
pub mod bytes;
2024-05-27 20:05:33 +00:00
pub mod content_disposition;
pub mod debug;
pub mod defer;
2024-09-30 06:46:54 +00:00
pub mod future;
2024-06-04 23:51:02 +00:00
pub mod hash;
2024-05-27 20:05:33 +00:00
pub mod html;
pub mod json;
pub mod math;
2024-06-14 21:11:31 +00:00
pub mod mutex_map;
2024-07-03 08:44:59 +00:00
pub mod rand;
2024-10-24 01:31:30 +00:00
pub mod result;
2024-08-08 17:18:30 +00:00
pub mod set;
pub mod stream;
2024-07-03 00:47:58 +00:00
pub mod string;
2024-05-27 20:05:33 +00:00
pub mod sys;
2025-01-08 20:43:02 +00:00
#[cfg(test)]
2024-06-25 01:31:52 +00:00
mod tests;
2024-07-03 00:57:50 +00:00
pub mod time;
2024-05-27 20:05:33 +00:00
2024-12-14 21:58:01 -05:00
pub use ::conduwuit_macros::implement;
2024-07-24 23:01:00 +00:00
pub use ::ctor::{ctor, dtor};
2024-08-08 17:18:30 +00:00
pub use self::{
2024-10-25 19:53:08 +00:00
arrayvec::ArrayVecExt,
2024-09-30 06:46:54 +00:00
bool::BoolExt,
2024-08-08 17:18:30 +00:00
bytes::{increment, u64_from_bytes, u64_from_u8, u64_from_u8x8},
debug::slice_truncated as debug_slice_truncated,
2024-09-30 06:46:54 +00:00
future::TryExtExt as TryFutureExtExt,
hash::sha256::delimited as calculate_hash,
2024-08-08 17:18:30 +00:00
html::Escape as HtmlEscape,
json::{deserialize_from_str, to_canonical_object},
math::clamp,
mutex_map::{Guard as MutexMapGuard, MutexMap},
2024-10-14 05:16:18 +00:00
rand::{shuffle, string as random_string},
2024-09-30 06:46:54 +00:00
stream::{IterStream, ReadyExt, Tools as StreamTools, TryReadyExt},
2024-08-08 17:18:30 +00:00
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,
},
2024-08-08 17:18:30 +00:00
};
2024-05-09 15:59:08 -07:00
2024-07-23 06:57:14 +00:00
#[inline]
pub fn exchange<T>(state: &mut T, source: T) -> T { std::mem::replace(state, source) }
2024-09-29 09:01:57 +00:00
2024-10-16 05:32:27 +00:00
#[macro_export]
macro_rules! extract_variant {
($e:expr, $variant:path) => {
match $e {
| $variant(value) => Some(value),
| _ => None,
2024-10-16 05:32:27 +00:00
}
};
}
#[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))
};
}
2024-09-29 09:01:57 +00:00
#[macro_export]
macro_rules! at {
($idx:tt) => {
|t| t.$idx
};
}
2024-12-02 11:54:03 +00:00
#[macro_export]
macro_rules! ref_at {
($idx:tt) => {
|ref t| &t.$idx
};
}
2025-01-14 05:55:49 +00:00
#[macro_export]
macro_rules! deref_at {
($idx:tt) => {
|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)
};
2024-12-01 10:49:46 +00:00
($($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
};
}