atm_io_utils/
macros.rs

1/// Helper macro for working with `io::Result` in a context where
2/// `ErrorKind::Interrupted` means to retry an action. This macro corresponds to
3/// `std::try`, but it will reevaluate the expression until it does not evaluate
4/// to an `Err` of kind `Interrupted`.
5#[macro_export]
6macro_rules! retry {
7    ($e:expr) => (
8        loop {
9            match $e {
10                Ok(t) => break t,
11                Err(ref e) if e.kind() == ::std::io::ErrorKind::Interrupted => {}
12                Err(e) => return Err(e.into()),
13            }
14        }
15    )
16}
17
18/// A variant of try_ready! that checks whether the expression evaluates to 0, and emits a
19/// `futures_io::Error` of kind `UnexpectedEof` with the given message if so.
20#[macro_export]
21macro_rules! read_nz {
22    ($e:expr, $msg:expr) => (
23        match $e {
24            Ok(::futures_core::Async::Ready(0)) => return Err(::futures_io::Error::new(::futures_io::ErrorKind::UnexpectedEof, $msg).into()),
25            Ok(::futures_core::Async::Ready(read)) => read,
26            Ok(::futures_core::Async::Pending) => return Ok(::futures_core::Async::Pending),
27            Err(e) => return Err(From::from(e)),
28        }
29    )
30}
31
32/// A variant of try_ready! that checks whether the expression evaluates to 0, and emits a
33/// `futures_io::Error` of kind `WriteZero` with the given message if so.
34#[macro_export]
35macro_rules! write_nz {
36    ($e:expr, $msg:expr) => (
37        match $e {
38            Ok(::futures_core::Async::Ready(0)) => return Err(::futures_io::Error::new(::futures_io::ErrorKind::WriteZero, $msg).into()),
39            Ok(::futures_core::Async::Ready(written)) => written,
40            Ok(::futures_core::Async::Pending) => return Ok(::futures_core::Async::Pending),
41            Err(e) => return Err(From::from(e)),
42        }
43    )
44}