5 releases

0.8.9 Oct 10, 2023
0.8.8 Oct 9, 2023
0.8.7 Jun 6, 2023
0.8.6 Feb 11, 2023
0.8.4 Sep 30, 2022

#156 in Asynchronous

Download history 269/week @ 2023-12-04 283/week @ 2023-12-11 370/week @ 2023-12-18 344/week @ 2023-12-25 333/week @ 2024-01-01 314/week @ 2024-01-08 4915/week @ 2024-01-15 5130/week @ 2024-01-22 12124/week @ 2024-01-29 6331/week @ 2024-02-05 7651/week @ 2024-02-12 1303/week @ 2024-02-19 1153/week @ 2024-02-26 1027/week @ 2024-03-04 575/week @ 2024-03-11 1587/week @ 2024-03-18

4,358 downloads per month
Used in 31 crates (via tokio_wasi)

MIT license

395KB
7K SLoC

Mio – Metal IO for WebAssembly

Mio is a fast, low-level I/O library for Rust focusing on non-blocking APIs and event notification for building high performance I/O apps with as little overhead as possible over the OS abstractions.

This crate is a fork from the original Mio library to support compilation into WebAssembly. The WebAssembly app can run inside the WasmEdge Runtime as a lightweight and secure alternative to natively compiled apps in Linux container.

Usage

To use mio, first add this to your Cargo.toml:

[dependencies]
mio_wasi = "0.8"

Next we can start using Mio. The following is quick introduction using TcpListener and TcpStream. Note that features = ["os-poll", "net"] must be specified for this example.

use std::error::Error;

use mio::net::{TcpListener, TcpStream};
use mio::{Events, Interest, Poll, Token};

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

fn main() -> Result<(), Box<dyn Error>> {
    // Create a poll instance.
    let mut poll = Poll::new()?;
    // Create storage for events.
    let mut events = Events::with_capacity(128);

    // Setup the server socket.
    let addr = "127.0.0.1:13265".parse()?;
    let mut server = TcpListener::bind(addr)?;
    // Start listening for incoming connections.
    poll.registry()
        .register(&mut server, SERVER, Interest::READABLE)?;

    // Setup the client socket.
    let mut client = TcpStream::connect(addr)?;
    // Register the socket.
    poll.registry()
        .register(&mut client, CLIENT, Interest::READABLE | Interest::WRITABLE)?;

    // Start an event loop.
    loop {
        // Poll Mio for events, blocking until we get an event.
        poll.poll(&mut events, None)?;

        // Process each event.
        for event in events.iter() {
            // We can use the token we previously provided to `register` to
            // determine for which socket the event is.
            match event.token() {
                SERVER => {
                    // If this is an event for the server, it means a connection
                    // is ready to be accepted.
                    //
                    // Accept the connection and drop it immediately. This will
                    // close the socket and notify the client of the EOF.
                    let connection = server.accept();
                    drop(connection);
                }
                CLIENT => {
                    if event.is_writable() {
                        // We can (likely) write to the socket without blocking.
                    }

                    if event.is_readable() {
                        // We can (likely) read from the socket without blocking.
                    }

                    // Since the server just shuts down the connection, let's
                    // just exit from our event loop.
                    return Ok(());
                }
                // We don't expect any events with tokens other than those we provided.
                _ => unreachable!(),
            }
        }
    }
}

Features

  • Non-blocking TCP, UDP
  • I/O event queue backed by epoll, kqueue, and IOCP
  • Zero allocations at runtime
  • Platform specific extensions

Non-goals

The following are specifically omitted from Mio and are left to the user or higher-level libraries.

  • File operations
  • Thread pools / multi-threaded event loop
  • Timers

Contributing

Interested in getting involved? We would love to help you! For simple bug fixes, just submit a PR with the fix and we can discuss the fix directly in the PR. If the fix is more complex, start with an issue.

If you want to propose an API change, create an issue to start a discussion with the community. Also, feel free to talk with us in Discord.

Finally, be kind. We support the Rust Code of Conduct.

Dependencies

~0–11MB
~92K SLoC