5个版本

0.8.9 2023年10月10日
0.8.8 2023年10月9日
0.8.7 2023年6月6日
0.8.6 2023年2月11日
0.8.4 2022年9月30日

#177 in 异步

Download history 5370/week @ 2024-04-22 954/week @ 2024-04-29 520/week @ 2024-05-06 1121/week @ 2024-05-13 864/week @ 2024-05-20 580/week @ 2024-05-27 986/week @ 2024-06-03 938/week @ 2024-06-10 1803/week @ 2024-06-17 575/week @ 2024-06-24 307/week @ 2024-07-01 1081/week @ 2024-07-08 450/week @ 2024-07-15 311/week @ 2024-07-22 315/week @ 2024-07-29 354/week @ 2024-08-05

1,456 每月下载量
29 个开源项目中使用 (通过 tokio_wasi)

MIT 许可证

395KB
7K SLoC

Mio – 适用于 WebAssembly 的金属 IO

Mio 是一个快速、低级的 Rust I/O 库,专注于非阻塞 API 和事件通知,以尽可能少的开销在操作系统抽象之上构建高性能 I/O 应用程序。

此包是从原始的 Mio 库分叉而来,以支持编译成 WebAssembly。WebAssembly 应用程序可以在 WasmEdge 运行时 中运行,作为 Linux 容器中本机编译应用程序的轻量级和安全的替代品。

用法

要使用 mio,首先在您的 Cargo.toml 中添加以下内容

[dependencies]
mio_wasi = "0.8"

接下来,我们可以开始使用 Mio。以下是一个使用 TcpListenerTcpStream 的快速入门示例。请注意,此示例需要指定 features = ["os-poll", "net"]

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!(),
            }
        }
    }
}

特性

  • 非阻塞 TCP、UDP
  • 由 epoll、kqueue 和 IOCP 支持的 I/O 事件队列
  • 运行时无零分配
  • 平台特定扩展

非目标

以下内容从 Mio 中特别省略,并留给用户或更高级的库。

  • 文件操作
  • 线程池/多线程事件循环
  • 计时器

贡献

有兴趣参与其中吗?我们很乐意帮助你!对于简单的错误修复,只需提交一个包含修复的 PR,我们可以在 PR 中直接讨论修复方案。如果修复更复杂,请先从问题开始。

如果您想提议API更改,请创建一个问题来开始与社区进行讨论。同时,也欢迎您在Discord与我们交流。

最后,请保持友善。我们支持Rust行为准则

依赖项

~0–11MB
~74K SLoC