You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1019 B

#[macro_export] macro_rules! static_assert {
($ex:expr) => {
const _: &[bool; ((($ex) == true) as usize)] = &[true];
}
}
4 years ago
#[macro_export] macro_rules! timeout {
($fut:expr, $dur:expr) => {
{
let dur = $dur;
tokio::select! {
output = $fut => {
Ok(output)
}
_ = tokio::time::delay_for(dur) => {
Err($crate::util::TimeoutError::from(dur))
}
}
}
}
}
/// Returned from timeout macro
#[derive(Debug)]
pub struct TimeoutError(tokio::time::Duration);
impl std::error::Error for TimeoutError{}
impl std::fmt::Display for TimeoutError
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
write!(f, "timeout of {} ms reached", self.0.as_millis())
}
}
impl From<tokio::time::Duration> for TimeoutError
{
fn from(from: tokio::time::Duration) -> Self
{
TimeoutError(from)
}
}
impl TimeoutError
{
/// Get the timeout that this error lapsed on
#[inline] pub fn timeout(&self) -> &tokio::time::Duration
{
&self.0
}
}