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