handle_connection_event just needs access to the Registry
@@ -4,7 +4,7 @@ use std::str::from_utf8;
use mio::event::Event;
use mio::net::{TcpListener, TcpStream};
-use mio::{Events, Interests, Poll, Token};
+use mio::{Events, Interests, Poll, Registry, Token};
// Setup some tokens to allow us to identify which event is for which socket.
const SERVER: Token = Token(0);
@@ -13,8 +13,6 @@ const SERVER: Token = Token(0);
const DATA: &[u8] = b"Hello world!\n";
fn main() -> io::Result<()> {
- env_logger::init();
-
// Create a poll instance.
let mut poll = Poll::new()?;
// Create storage for events.
@@ -60,7 +58,7 @@ fn main() -> io::Result<()> {
token => {
// (maybe) received an event for a TCP connection.
let done = if let Some(connection) = connections.get_mut(&token) {
- handle_connection_event(&mut poll, connection, event)?
+ handle_connection_event(poll.registry(), connection, event)?
} else {
// Sporadic events happen.
false
@@ -82,7 +80,7 @@ fn next(current: &mut Token) -> Token {
/// Returns `true` if the connection is done.
fn handle_connection_event(
- poll: &mut Poll,
+ registry: &Registry,
connection: &mut TcpStream,
event: &Event,
) -> io::Result<bool> {
@@ -96,7 +94,7 @@ fn handle_connection_event(
Ok(_) => {
// After we've written something we'll reregister the connection
// to only respond to readable events.
- poll.registry()
+ registry
.reregister(&connection, event.token(), Interests::READABLE)?
}
// Would block "errors" are the OS's way of saying that the
@@ -104,7 +102,7 @@ fn handle_connection_event(
Err(ref err) if would_block(err) => {}
// Got interrupted (how rude!), we'll try again.
Err(ref err) if interrupted(err) => {
- return handle_connection_event(poll, connection, event)
+ return handle_connection_event(registry, connection, event)
}
// Other errors we'll consider fatal.
Err(err) => return Err(err),
