#[macro_export] macro_rules! static_assert { ($ex:expr) => { const _: &[bool; ((($ex) == true) as usize)] = &[true]; } } #[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 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 } }