Add timeout macro to simplify timeout logic

This commit is contained in:
Jeremy Soller
2020-02-20 21:09:10 -07:00
parent 85e7225e95
commit 369a695bd9
5 changed files with 76 additions and 44 deletions

View File

@ -1,3 +1,26 @@
#[macro_export]
macro_rules! timeout {
($t:expr, $f:expr) => {{
let mut result = Err($crate::Error::Timeout);
while $t.running() {
match $f {
Ok(ok) => {
result = Ok(ok);
break;
},
Err(err) => match err {
$crate::Error::WouldBlock => (),
_ => {
result = Err(err);
break;
}
},
}
}
result
}};
}
pub trait Timeout {
fn reset(&mut self);
fn running(&self) -> bool;