mio_misc/
poll.rs

1//! Wrapper around `mio::Poll` to offer a slightly more convenient API
2use mio::Registry;
3use std::io;
4use std::time::Duration;
5
6/// Encapsulates `mio::Poll` and `mio::Events`
7#[derive(Debug)]
8pub struct Poll {
9    poll: mio::Poll,
10    events: mio::Events,
11}
12
13impl Poll {
14    /// Creates a `Poll`
15    pub fn with_capacity(capacity: usize) -> io::Result<Poll> {
16        let poll = mio::Poll::new()?;
17        Ok(Poll {
18            poll,
19            events: mio::Events::with_capacity(capacity),
20        })
21    }
22
23    /// Polls until the optionally provided duration and returns `Events`
24    pub fn poll<I>(&mut self, timeout: I) -> io::Result<&mio::Events>
25    where
26        I: Into<Option<Duration>>,
27    {
28        self.poll.poll(&mut self.events, timeout.into())?;
29        Ok(&self.events)
30    }
31
32    /// Clears polled events
33    pub fn clear(&mut self) {
34        self.events.clear()
35    }
36
37    /// Gives access to polled events
38    pub fn polled_events(&self) -> &mio::Events {
39        &self.events
40    }
41    /// Returns registry
42    pub fn registry(&self) -> &Registry {
43        self.poll.registry()
44    }
45}