]>
git.proxmox.com Git - rustc.git/blob - vendor/mio/src/token.rs
1 /// Associates readiness events with [`event::Source`]s.
3 /// `Token` is a wrapper around `usize` and is used as an argument to
4 /// [`Registry::register`] and [`Registry::reregister`].
6 /// See [`Poll`] for more documentation on polling.
8 /// [`event::Source`]: ./event/trait.Source.html
9 /// [`Poll`]: struct.Poll.html
10 /// [`Registry::register`]: struct.Registry.html#method.register
11 /// [`Registry::reregister`]: struct.Registry.html#method.reregister
15 /// Using `Token` to track which socket generated the event. In this example,
16 /// `HashMap` is used, but usually something like [`slab`] is better.
18 /// [`slab`]: https://crates.io/crates/slab
20 #[cfg_attr(all(feature = "os-poll", feature = "net"), doc = "```")]
21 #[cfg_attr(not(all(feature = "os-poll", feature = "net")), doc = "```ignore")]
22 /// # use std::error::Error;
23 /// # fn main() -> Result<(), Box<dyn Error>> {
24 /// use mio::{Events, Interest, Poll, Token};
25 /// use mio::net::TcpListener;
28 /// use std::io::{self, Read};
29 /// use std::collections::HashMap;
31 /// // After this number of sockets is accepted, the server will shutdown.
32 /// const MAX_SOCKETS: usize = 32;
34 /// // Pick a token that will not be used by any other socket and use that one
35 /// // for the listener.
36 /// const LISTENER: Token = Token(1024);
38 /// // Used to store the sockets.
39 /// let mut sockets = HashMap::new();
41 /// // This is used to generate a unique token for a socket
42 /// let mut next_socket_index = 0;
44 /// // The `Poll` instance
45 /// let mut poll = Poll::new()?;
48 /// let mut listener = TcpListener::bind("127.0.0.1:0".parse()?)?;
50 /// // Register the listener
51 /// poll.registry().register(&mut listener, LISTENER, Interest::READABLE)?;
53 /// // Spawn a thread that will connect a bunch of sockets then close them
54 /// let addr = listener.local_addr()?;
55 /// thread::spawn(move || {
56 /// use std::net::TcpStream;
58 /// // +1 here is to connect an extra socket to signal the socket to close
59 /// for _ in 0..(MAX_SOCKETS+1) {
60 /// // Connect then drop the socket
61 /// let _ = TcpStream::connect(addr).unwrap();
66 /// let mut events = Events::with_capacity(1024);
68 /// // Read buffer, this will never actually get filled
69 /// let mut buf = [0; 256];
71 /// // The main event loop
73 /// // Wait for events
74 /// poll.poll(&mut events, None)?;
76 /// for event in &events {
77 /// match event.token() {
79 /// // Perform operations in a loop until `WouldBlock` is
82 /// match listener.accept() {
83 /// Ok((mut socket, _)) => {
84 /// // Shutdown the server
85 /// if next_socket_index == MAX_SOCKETS {
89 /// // Get the token for the socket
90 /// let token = Token(next_socket_index);
91 /// next_socket_index += 1;
93 /// // Register the new socket w/ poll
94 /// poll.registry().register(&mut socket, token, Interest::READABLE)?;
96 /// // Store the socket
97 /// sockets.insert(token, socket);
99 /// Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
100 /// // Socket is not ready anymore, stop accepting
103 /// e => panic!("err={:?}", e), // Unexpected error
108 /// // Always operate in a loop
110 /// match sockets.get_mut(&token).unwrap().read(&mut buf) {
112 /// // Socket is closed, remove it from the map
113 /// sockets.remove(&token);
116 /// // Data is not actually sent in this example
117 /// Ok(_) => unreachable!(),
118 /// Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
119 /// // Socket is not ready anymore, stop reading
122 /// e => panic!("err={:?}", e), // Unexpected error
131 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
132 pub struct Token(pub usize);
134 impl From
<Token
> for usize {
135 fn from(val
: Token
) -> usize {