queen_io/sys/
mod.rs

1// Helper macro to execute a system call that returns an `io::Result`.
2macro_rules! syscall {
3    ($fn: ident ( $($arg: expr),* $(,)* ) ) => {{
4        let res = unsafe { libc::$fn($($arg, )*) };
5        if res == -1 {
6            Err(io::Error::last_os_error())
7        } else {
8            Ok(res)
9        }
10    }};
11}
12
13use std::io::{self, ErrorKind};
14
15pub use self::epoll::{Epoll, Events};
16
17pub mod fd;
18pub mod epoll;
19pub mod timerfd;
20pub mod eventfd;
21pub mod socket;
22
23pub trait IsMinusOne {
24    fn is_minus_one(&self) -> bool;
25}
26
27impl IsMinusOne for i32 {
28    fn is_minus_one(&self) -> bool { *self == -1 }
29}
30impl IsMinusOne for isize {
31    fn is_minus_one(&self) -> bool { *self == -1 }
32}
33
34pub fn cvt<T: IsMinusOne>(t: T) -> ::std::io::Result<T> {
35    if t.is_minus_one() {
36        Err(io::Error::last_os_error())
37    } else {
38        Ok(t)
39    }
40}
41
42pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
43    where T: IsMinusOne,
44          F: FnMut() -> T
45{
46    loop {
47        match cvt(f()) {
48            Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
49            other => return other,
50        }
51    }
52}