Skip to main content

externally_driven_async_rt/selector/
mod.rs

1mod direction;
2#[cfg(miri)]
3mod dummy;
4#[cfg(all(any(target_os = "linux", target_os = "android"), not(miri)))]
5mod epoll;
6mod io_change;
7mod kernel_timer;
8#[cfg(all(target_os = "macos", not(miri)))]
9mod kqueue;
10mod selector_event;
11mod timer_change;
12
13#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "android")))]
14compile_error!("externally-driven-async-rt supports only macOS, Linux and Android");
15
16use rustix::fd::{AsFd, BorrowedFd};
17
18#[cfg(miri)]
19use self::dummy::DummySelector as Backend;
20#[cfg(all(any(target_os = "linux", target_os = "android"), not(miri)))]
21use self::epoll::EpollSelector as Backend;
22#[cfg(all(target_os = "macos", not(miri)))]
23use self::kqueue::KqueueSelector as Backend;
24pub(crate) use self::{
25    direction::{Direction, Directions},
26    io_change::IoChange,
27    kernel_timer::KernelTimer,
28    selector_event::{EVENTS_PER_CALL, SelectorEvent},
29    timer_change::TimerChange,
30};
31use crate::time::Instant;
32
33/// A selector for your current platform (epoll on Linux and kqueue on macOS).
34#[must_use]
35pub struct Selector {
36    backend: Backend,
37}
38
39impl Selector {
40    /// Constructor.
41    ///
42    /// # Errors
43    ///
44    /// Returns an error if an epoll/kqueue syscall fails.
45    pub fn new() -> rustix::io::Result<Self> {
46        Ok(Self {
47            backend: Backend::new()?,
48        })
49    }
50
51    pub(crate) fn apply_now(&self, change: IoChange) -> rustix::io::Result<()> {
52        self.backend.apply_now(change)
53    }
54
55    pub(crate) fn notify(&self) -> rustix::io::Result<()> {
56        self.backend.notify()
57    }
58
59    pub(crate) fn flush(
60        &self,
61        io_changes: impl IntoIterator<Item = IoChange>,
62        deadline: Option<Instant>,
63        now: Instant,
64    ) -> rustix::io::Result<()> {
65        self.backend.flush(io_changes, deadline, now)
66    }
67
68    pub(crate) fn poll_nonblocking(
69        &self,
70        out: &mut [Option<SelectorEvent>; EVENTS_PER_CALL],
71    ) -> rustix::io::Result<bool> {
72        self.backend.poll_nonblocking(out)
73    }
74
75    pub(crate) fn as_fd(&self) -> BorrowedFd<'_> {
76        self.backend.as_fd()
77    }
78}