externally_driven_async_rt/selector/
mod.rs1mod direction;
2#[cfg(target_os = "linux")]
3mod epoll;
4mod io_change;
5mod kernel_timer;
6#[cfg(target_os = "macos")]
7mod kqueue;
8mod selector_event;
9mod timer_change;
10mod token;
11
12#[cfg(not(any(target_os = "macos", target_os = "linux")))]
13compile_error!("externally-driven-async-rt supports only macOS and Linux");
14
15use rustix::fd::{AsFd, BorrowedFd};
16
17#[cfg(target_os = "linux")]
18use self::epoll::EpollSelector as Backend;
19#[cfg(target_os = "macos")]
20use self::kqueue::KqueueSelector as Backend;
21pub(crate) use self::{
22 direction::{Direction, Directions},
23 io_change::IoChange,
24 kernel_timer::KernelTimer,
25 selector_event::{EVENTS_PER_CALL, SelectorEvent},
26 timer_change::TimerChange,
27 token::Token,
28};
29use crate::time::Instant;
30
31pub struct Selector {
33 backend: Backend,
34}
35
36impl Selector {
37 pub fn new() -> rustix::io::Result<Self> {
39 Ok(Self {
40 backend: Backend::new()?,
41 })
42 }
43
44 pub(crate) fn apply_now(&self, change: IoChange) -> rustix::io::Result<()> {
45 self.backend.apply_now(change)
46 }
47
48 pub(crate) fn arm_timer(&self, deadline: Instant) {
49 self.backend.arm_timer(deadline);
50 }
51
52 pub(crate) fn disarm_timer(&self) {
53 self.backend.disarm_timer();
54 }
55
56 pub(crate) fn flush(
57 &self,
58 io_changes: impl IntoIterator<Item = IoChange>,
59 ) -> rustix::io::Result<()> {
60 self.backend.flush(io_changes)
61 }
62
63 pub(crate) fn poll_nonblocking(
64 &self,
65 out: &mut [Option<SelectorEvent>; EVENTS_PER_CALL],
66 ) -> rustix::io::Result<bool> {
67 self.backend.poll_nonblocking(out)
68 }
69
70 pub(crate) fn as_fd(&self) -> BorrowedFd<'_> {
71 self.backend.as_fd()
72 }
73}
74
75impl core::fmt::Debug for Selector {
76 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77 f.write_str("Selector")
78 }
79}
80
81#[cfg(test)]
82mod tests;