1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use file::*;
use err::*;
use tempus::Span;

#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct Poll {
    pub fd: ::libc::c_int,
    pub ev: Event,
    r: Event,
}

impl Poll {
    #[inline]
    pub fn new(f: &File, ev: Event) -> Self { Poll {
        fd: f.fd() as _, ev: ev, r: Event::empty(),
    } }

    #[inline]
    pub fn ready(&self) -> Event { self.r }
}

bitflags! {
    pub struct Event: ::libc::c_short {
        const In  = ::libc::POLLIN;
        const Out = ::libc::POLLOUT;
        const Pri = ::libc::POLLPRI;
        const Hup = ::libc::POLLHUP;
        const Err = ::libc::POLLERR;
    }
}

pub trait PollExt {
    fn poll(&mut self, t: Option<Span>) -> Result<usize, OsErr>;
}

impl PollExt for [Poll] {
    #[inline]
    fn poll(&mut self, t: Option<Span>) -> Result<usize, OsErr> {
        let t = match t {
            None => None,
            Some(t) => Some(t.to_c_timespec().ok_or(ERANGE)?),
        };
        unsafe { esyscall!(POLL, self.as_ptr(), self.len(), t.as_ref().map_or(::core::ptr::null(), |p| p as *const _), 0) }
    }
}