mod actix_try_join_all;
use std::time::Instant;
use chrono::{DateTime, Utc};
use derive_more::From;
use futures::channel::mpsc::{TrySendError, UnboundedSender};
use rand::{distributions::Alphanumeric, Rng};
pub use self::actix_try_join_all::actix_try_join_all;
#[macro_export]
macro_rules! hashmap {
(@single $($x:tt)*) => (());
(@count $($rest:expr),*) => (<[()]>::len(&[$(hashmap!(@single $rest)),*]));
($($key:expr => $value:expr,)+) => { hashmap!($($key => $value),+) };
($($key:expr => $value:expr),*) => {
{
let _cap = hashmap!(@count $($key),*);
let mut _map = ::std::collections::HashMap::with_capacity(_cap);
$(
let _ = _map.insert($key, $value);
)*
_map
}
};
}
#[macro_export]
macro_rules! hashset {
(@single $($x:tt)*) => (());
(@count $($rest:expr),*) => (<[()]>::len(&[$(hashset!(@single $rest)),*]));
($($value:expr,)+) => { hashset!($($value),+) };
($($value:expr),*) => {
{
let _cap = hashset!(@count $($value),*);
let mut _map = ::std::collections::HashSet::with_capacity(_cap);
$(
let _ = _map.insert($value);
)*
_map
}
};
}
#[macro_export]
macro_rules! impl_debug_by_struct_name {
($mock:ty) => {
impl ::std::fmt::Debug for $mock {
fn fmt(
&self,
f: &mut ::std::fmt::Formatter<'_>,
) -> ::std::result::Result<(), ::std::fmt::Error> {
f.debug_struct(stringify!($mock)).finish()
}
}
};
}
#[macro_export]
macro_rules! actix_try {
($e:expr) => {
match $e {
Ok(p) => p,
Err(e) => {
return Box::pin(actix::fut::err(e.into()));
}
};
};
}
#[inline]
#[must_use]
pub fn instant_into_utc(instant: Instant) -> DateTime<Utc> {
chrono::Duration::from_std(instant.elapsed())
.map_or_else(|_| Utc::now(), |dur| Utc::now() - dur)
}
#[must_use]
pub fn generate_token(length: usize) -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(length)
.map(char::from)
.collect()
}
#[derive(Clone, Debug, From)]
pub struct MpscOneshotSender<T>(UnboundedSender<T>);
impl<T> MpscOneshotSender<T> {
pub fn send(self, message: T) -> Result<(), TrySendError<T>> {
self.0.unbounded_send(message)
}
}