mio/sys/
mod.rs

1//! Module with system specific types.
2//!
3//! Required types:
4//!
5//! * `Event`: a type alias for the system specific event, e.g. `kevent` or
6//!   `epoll_event`.
7//! * `event`: a module with various helper functions for `Event`, see
8//!   [`crate::event::Event`] for the required functions.
9//! * `Events`: collection of `Event`s, see [`crate::Events`].
10//! * `IoSourceState`: state for the `IoSource` type.
11//! * `Selector`: selector used to register event sources and poll for events,
12//!   see [`crate::Poll`] and [`crate::Registry`] for required methods.
13//! * `tcp` and `udp` modules: see the [`crate::net`] module.
14//! * `Waker`: see [`crate::Waker`].
15
16cfg_os_poll! {
17    macro_rules! debug_detail {
18        (
19            $type: ident ($event_type: ty), $test: path,
20            $($(#[$target: meta])* $libc: ident :: $flag: ident),+ $(,)*
21        ) => {
22            struct $type($event_type);
23
24            impl fmt::Debug for $type {
25                fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26                    let mut written_one = false;
27                    $(
28                        $(#[$target])*
29                        #[allow(clippy::bad_bit_mask)] // Apparently some flags are zero.
30                        {
31                            // Windows doesn't use `libc` but the `afd` module.
32                            if $test(&self.0, &$libc :: $flag) {
33                                if !written_one {
34                                    write!(f, "{}", stringify!($flag))?;
35                                    written_one = true;
36                                } else {
37                                    write!(f, "|{}", stringify!($flag))?;
38                                }
39                            }
40                        }
41                    )+
42                    if !written_one {
43                        write!(f, "(empty)")
44                    } else {
45                        Ok(())
46                    }
47                }
48            }
49        };
50    }
51}
52
53#[cfg(any(unix, target_os = "hermit"))]
54cfg_os_poll! {
55    mod unix;
56    #[allow(unused_imports)]
57    pub use self::unix::*;
58}
59
60#[cfg(windows)]
61cfg_os_poll! {
62    mod windows;
63    pub use self::windows::*;
64}
65
66#[cfg(target_os = "wasi")]
67cfg_os_poll! {
68    mod wasi;
69    pub(crate) use self::wasi::*;
70}
71
72cfg_not_os_poll! {
73    mod shell;
74    pub(crate) use self::shell::*;
75
76    #[cfg(unix)]
77    cfg_any_os_ext! {
78        mod unix;
79        #[cfg(feature = "os-ext")]
80        pub use self::unix::SourceFd;
81    }
82}
83
84/// Define the `listen` backlog parameters as in the standard library. This
85/// helps avoid hardcoded unsynchronized values and allows better control of
86/// default values depending on the target.
87///
88/// Selecting a “valid” default value can be tricky due to:
89///
90/// - It often serves only as a hint and may be rounded, trimmed, or ignored by
91///   the OS.
92///
93/// - It is sometimes provided as a "magic" value, for example, -1. This
94///   value is undocumented and not standard, but it is often used to represent
95///   the largest possible backlog size. This happens due to signed/unsigned
96///   conversion and rounding to the upper bound performed by the OS.
97///
98/// - Default values vary depending on the OS and its version. Common defaults
99///   include: -1, 128, 1024, and 4096.
100///
101// Here is the original code from the standard library
102// https://github.com/rust-lang/rust/blob/4f808ba6bf9f1c8dde30d009e73386d984491587/library/std/src/os/unix/net/listener.rs#L72
103//
104#[allow(dead_code)]
105#[cfg(any(
106    target_os = "windows",
107    target_os = "redox",
108    target_os = "espidf",
109    target_os = "horizon"
110))]
111pub(crate) const LISTEN_BACKLOG_SIZE: i32 = 128;
112
113/// This is a special case for some target(s) supported by `mio`.  This value
114/// is needed because `libc::SOMAXCON` (used as a fallback for unknown targets)
115/// is not implemented for them. Feel free to update this if the `libc` crate
116/// changes.
117#[allow(dead_code)]
118#[cfg(target_os = "hermit")]
119pub(crate) const LISTEN_BACKLOG_SIZE: i32 = 1024;
120
121#[allow(dead_code)]
122#[cfg(any(
123    // Silently capped to `/proc/sys/net/core/somaxconn`.
124    target_os = "linux",
125    // Silently capped to `kern.ipc.soacceptqueue`.
126    target_os = "freebsd",
127    // Silently capped to `kern.somaxconn sysctl`.
128    target_os = "openbsd",
129    // Silently capped to the default 128.
130    target_vendor = "apple",
131))]
132pub(crate) const LISTEN_BACKLOG_SIZE: i32 = -1;
133
134#[allow(dead_code)]
135#[cfg(not(any(
136    target_os = "windows",
137    target_os = "redox",
138    target_os = "espidf",
139    target_os = "horizon",
140    target_os = "linux",
141    target_os = "freebsd",
142    target_os = "openbsd",
143    target_os = "wasi",
144    target_os = "hermit",
145    target_vendor = "apple",
146)))]
147pub(crate) const LISTEN_BACKLOG_SIZE: i32 = libc::SOMAXCONN;