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-06-04 23:51:02 +00:00
|
|
|
pub mod hash;
|
2024-05-27 20:05:33 +00:00
|
|
|
pub mod html;
|
|
|
|
pub mod json;
|
2024-07-07 03:36:50 +00:00
|
|
|
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-07-03 00:47:58 +00:00
|
|
|
pub mod string;
|
2024-05-27 20:05:33 +00:00
|
|
|
pub mod sys;
|
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-07-03 00:57:50 +00:00
|
|
|
use std::cmp::{self, Ordering};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-03 00:54:36 +00:00
|
|
|
pub use bytes::{increment, u64_from_bytes, u64_from_u8, u64_from_u8x8};
|
2024-05-27 20:05:33 +00:00
|
|
|
pub use debug::slice_truncated as debug_slice_truncated;
|
2024-07-03 00:44:00 +00:00
|
|
|
pub use hash::calculate_hash;
|
2024-05-27 20:05:33 +00:00
|
|
|
pub use html::Escape as HtmlEscape;
|
|
|
|
pub use json::{deserialize_from_str, to_canonical_object};
|
2024-07-09 20:04:43 +00:00
|
|
|
pub use mutex_map::{Guard as MutexMapGuard, MutexMap};
|
2024-07-03 08:44:59 +00:00
|
|
|
pub use rand::string as random_string;
|
|
|
|
pub use string::{str_from_bytes, string_from_bytes};
|
2024-05-27 20:05:33 +00:00
|
|
|
pub use sys::available_parallelism;
|
2024-07-03 19:13:49 +00:00
|
|
|
pub use time::now_millis as millis_since_unix_epoch;
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-23 06:57:14 +00:00
|
|
|
#[inline]
|
2024-05-09 15:59:08 -07:00
|
|
|
pub fn clamp<T: Ord>(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) }
|
2024-03-29 18:28:04 -07:00
|
|
|
|
2024-07-23 06:57:14 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn exchange<T: Clone>(state: &mut T, source: T) -> T {
|
|
|
|
let ret = state.clone();
|
|
|
|
*state = source;
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
#[must_use]
|
|
|
|
pub fn generate_keypair() -> Vec<u8> {
|
2024-07-03 08:44:59 +00:00
|
|
|
let mut value = rand::string(8).as_bytes().to_vec();
|
2021-06-08 18:10:00 +02:00
|
|
|
value.push(0xFF);
|
|
|
|
value.extend_from_slice(
|
|
|
|
&ruma::signatures::Ed25519KeyPair::generate().expect("Ed25519KeyPair generation always works (?)"),
|
|
|
|
);
|
|
|
|
value
|
2020-04-19 14:14:47 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
#[allow(clippy::impl_trait_in_params)]
|
|
|
|
pub fn common_elements(
|
2021-04-09 21:38:16 +02:00
|
|
|
mut iterators: impl Iterator<Item = impl Iterator<Item = Vec<u8>>>, check_order: impl Fn(&[u8], &[u8]) -> Ordering,
|
|
|
|
) -> Option<impl Iterator<Item = Vec<u8>>> {
|
2020-08-21 21:22:59 +02:00
|
|
|
let first_iterator = iterators.next()?;
|
2024-03-08 09:25:47 -05:00
|
|
|
let mut other_iterators = iterators.map(Iterator::peekable).collect::<Vec<_>>();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2020-08-21 21:22:59 +02:00
|
|
|
Some(first_iterator.filter(move |target| {
|
2021-06-17 20:34:14 +02:00
|
|
|
other_iterators.iter_mut().all(|it| {
|
|
|
|
while let Some(element) = it.peek() {
|
|
|
|
match check_order(element, target) {
|
|
|
|
Ordering::Greater => return false, // We went too far
|
|
|
|
Ordering::Equal => return true, // Element is in both iters
|
|
|
|
Ordering::Less => {
|
|
|
|
// Keep searching
|
|
|
|
it.next();
|
2020-08-21 21:22:59 +02:00
|
|
|
},
|
|
|
|
}
|
2021-06-17 20:34:14 +02:00
|
|
|
}
|
|
|
|
false
|
|
|
|
})
|
2020-08-21 21:22:59 +02:00
|
|
|
}))
|
|
|
|
}
|