Crate mio [] [src]

A fast, low-level IO library for Rust focusing on non-blocking APIs, event notification, and other useful utilities for building high performance IO apps.

Goals

Usage

Using mio starts by creating an EventLoop, which handles receiving events from the OS and dispatching them to a supplied Handler.

Example

use mio::*;
use mio::tcp::{TcpListener, TcpStream};

// Setup some tokens to allow us to identify which event is
// for which socket.
const SERVER: Token = Token(0);
const CLIENT: Token = Token(1);

let addr = "127.0.0.1:13265".parse().unwrap();

// Setup the server socket
let server = TcpListener::bind(&addr).unwrap();

// Create an event loop
let mut event_loop = EventLoop::new().unwrap();

// Start listening for incoming connections
event_loop.register(&server, SERVER).unwrap();

// Setup the client socket
let sock = TcpStream::connect(&addr).unwrap();

// Register the socket
event_loop.register(&sock, CLIENT).unwrap();

// Define a handler to process the events
struct MyHandler(TcpListener);

impl Handler for MyHandler {
    type Timeout = ();
    type Message = ();

    fn ready(&mut self, event_loop: &mut EventLoop<MyHandler>, token: Token, _: EventSet) {
        match token {
            SERVER => {
                let MyHandler(ref mut server) = *self;
                // Accept and drop the socket immediately, this will close
                // the socket and notify the client of the EOF.
                let _ = server.accept();
            }
            CLIENT => {
                // The server just shuts down the socket, let's just
                // shutdown the event loop
                event_loop.shutdown();
            }
            _ => panic!("unexpected token"),
        }
    }
}

// Start handling events
event_loop.run(&mut MyHandler(server)).unwrap();

Reexports

pub use sys::{Io, Selector};

Modules

buf
prelude
tcp
udp
unix
util

Utilities for non-blocking IO programs

Structs

EventLoop

Single threaded IO event loop.

EventLoopConfig

Configure EventLoop runtime details

EventSet
Ipv4Addr

Representation of an IPv4 address.

Ipv6Addr

Representation of an IPv6 address.

Poll
PollOpt
Sender

Sends messages to the EventLoop from other threads.

Timeout
TimerError
Token

Enums

IpAddr

An IP address, either a IPv4 or IPv6 address.

NotifyError

Traits

Buf

A trait for values that provide sequential read access to bytes.

Evented

A value that may be registered with an EventLoop

Handler
MutBuf

A trait for values that provide sequential write access to bytes.

TryAccept
TryRead
TryWrite

Type Definitions

TimerResult

Implementations