Debian Codegraph @

debian/rust-vendor/mio/README.md

raw
1 # Mio – Metal I/O
2
3 Mio is a fast, low-level I/O library for Rust focusing on non-blocking APIs and
4 event notification for building high performance I/O apps with as little
5 overhead as possible over the OS abstractions.
6
7 [![Crates.io][crates-badge]][crates-url]
8 [![MIT licensed][mit-badge]][mit-url]
9 [![Build Status][actions-badge]][actions-url]
10
11 [crates-badge]: https://img.shields.io/crates/v/mio.svg
12 [crates-url]: https://crates.io/crates/mio
13 [mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
14 [mit-url]: LICENSE
15 [actions-badge]: https://github.com/tokio-rs/mio/workflows/CI/badge.svg
16 [actions-url]: https://github.com/tokio-rs/mio/actions?query=workflow%3ACI+branch%3Amaster
17
18 **API documentation**
19
20 * [v1](https://docs.rs/mio/^1)
21 * [v0.8](https://docs.rs/mio/^0.8)
22
23 This is a low level library, if you are looking for something easier to get
24 started with, see [Tokio](https://tokio.rs).
25
26 ## Usage
27
28 To use `mio`, first add this to your `Cargo.toml`:
29
30 ```toml
31 [dependencies]
32 mio = "1"
33 ```
34
35 Next we can start using Mio. The following is a quick introduction using
36 `TcpListener` and `TcpStream`. Note that `features = ["os-poll", "net"]` must be
37 specified for this example.
38
39 ```rust
40 use std::error::Error;
41
42 use mio::net::{TcpListener, TcpStream};
43 use mio::{Events, Interest, Poll, Token};
44
45 // Some tokens to allow us to identify which event is for which socket.
46 const SERVER: Token = Token(0);
47 const CLIENT: Token = Token(1);
48
49 fn main() -> Result<(), Box<dyn Error>> {
50 // Create a poll instance.
51 let mut poll = Poll::new()?;
52 // Create storage for events.
53 let mut events = Events::with_capacity(128);
54
55 // Setup the server socket.
56 let addr = "127.0.0.1:13265".parse()?;
57 let mut server = TcpListener::bind(addr)?;
58 // Start listening for incoming connections.
59 poll.registry()
60 .register(&mut server, SERVER, Interest::READABLE)?;
61
62 // Setup the client socket.
63 let mut client = TcpStream::connect(addr)?;
64 // Register the socket.
65 poll.registry()
66 .register(&mut client, CLIENT, Interest::READABLE | Interest::WRITABLE)?;
67
68 // Start an event loop.
69 loop {
70 // Poll Mio for events, blocking until we get an event.
71 poll.poll(&mut events, None)?;
72
73 // Process each event.
74 for event in events.iter() {
75 // We can use the token we previously provided to `register` to
76 // determine for which socket the event is.
77 match event.token() {
78 SERVER => {
79 // If this is an event for the server, it means a connection
80 // is ready to be accepted.
81 //
82 // Accept the connection and drop it immediately. This will
83 // close the socket and notify the client of the EOF.
84 let connection = server.accept();
85 drop(connection);
86 }
87 CLIENT => {
88 if event.is_writable() {
89 // We can (likely) write to the socket without blocking.
90 }
91
92 if event.is_readable() {
93 // We can (likely) read from the socket without blocking.
94 }
95
96 // Since the server just shuts down the connection, let's
97 // just exit from our event loop.
98 return Ok(());
99 }
100 // We don't expect any events with tokens other than those we provided.
101 _ => unreachable!(),
102 }
103 }
104 }
105 }
106 ```
107
108 ## Features
109
110 * Non-blocking TCP, UDP, UDS
111 * I/O event queue backed by epoll, kqueue, and IOCP
112 * Zero allocations at runtime
113 * Platform specific extensions
114
115 ## Non-goals
116
117 The following are specifically omitted from Mio and are left to the user
118 or higher-level libraries.
119
120 * File operations
121 * Thread pools / multi-threaded event loop
122 * Timers
123
124 ## Platforms
125
126 Currently supported platforms:
127
128 * Android (API level 21)
129 * DragonFly BSD
130 * FreeBSD
131 * Linux
132 * NetBSD
133 * OpenBSD
134 * Windows
135 * iOS
136 * macOS
137 * Solaris
138
139 Mio can handle interfacing with each of the event systems of the aforementioned
140 platforms. The details of their implementation are further discussed in the
141 `Poll` type of the API documentation (see above).
142
143 Mio generally supports the same versions of the above mentioned platforms as
144 Rust the language (rustc) does, unless otherwise noted.
145
146 The Windows implementation for polling sockets is using the [wepoll] strategy.
147 This uses the Windows AFD system to access socket readiness events.
148
149 [wepoll]: https://github.com/piscisaureus/wepoll
150
151 ### Unsupported
152
153 * Wine, see [issue #1444]
154
155 [issue #1444]: https://github.com/tokio-rs/mio/issues/1444
156
157 ## MSRV Policy
158
159 The MSRV (Minimum Supported Rust Version) is fixed for a given minor (1.x)
160 version. However it can be increased when bumping minor versions, i.e. going
161 from 1.0 to 1.1 allows us to increase the MSRV. Users unable to increase their
162 Rust version can use an older minor version instead. Below is a list of Mio versions
163 and their MSRV:
164
165 * v0.8: Rust 1.46.
166 * v1.0: Rust 1.70.
167 * v1.1: Rust 1.71.
168 * v1.2: Rust 1.71.
169
170 Note however that Mio also has dependencies, which might have different MSRV
171 policies. We try to stick to the above policy when updating dependencies, but
172 this is not always possible.
173
174 ## Unsupported flags
175
176 Mio uses different implementations to support the same functionality depending
177 on the platform. Mio generally uses the "best" implementation possible, where
178 "best" usually means most efficient for Mio's use case. However this means that
179 the implementation is often specific to a limited number of platforms, meaning
180 we often have multiple implementations for the same functionality. In some cases
181 it might be required to not use the "best" implementation, but another
182 implementation Mio supports (on other platforms). **Mio does not officially
183 support secondary implementations on platforms**, however we do have various cfg
184 flags to force another implementation for these situations.
185
186 Current flags:
187 * `mio_unsupported_force_poll_poll`, uses an implementation based on `poll(2)`
188 for `mio::Poll`.
189 * `mio_unsupported_force_waker_pipe`, uses an implementation based on `pipe(2)`
190 for `mio::Waker`.
191
192 **Again, Mio does not officially supports this**. Furthermore these flags may
193 disappear in the future.
194
195 ## Community
196
197 A group of Mio users hang out on [Discord], this can be a good place to go for
198 questions. It's also possible to open a [new issue on GitHub] to ask questions,
199 report bugs or suggest new features.
200
201 [Discord]: https://discord.gg/tokio
202 [new issue on GitHub]: https://github.com/tokio-rs/mio/issues/new
203
204 ## Contributing
205
206 Interested in getting involved? We would love to help you! For simple
207 bug fixes, just submit a PR with the fix and we can discuss the fix
208 directly in the PR. If the fix is more complex, start with an issue.
209
210 If you want to propose an API change, create an issue to start a
211 discussion with the community. Also, feel free to talk with us in Discord.
212
213 Finally, be kind. We support the [Rust Code of Conduct](https://www.rust-lang.org/policies/code-of-conduct).