Nio
Thread-Per-Core async runtime for Rust
Install / Use
/learn @nurmohammed840/NioREADME
Nio
Nio is a Thread-Per-Core async runtime for Rust.
Task spawning APIs
Nio uses multiple worker threads to execute tasks.
| Function | Send requirement | Thread affinity |
| ---------------------- | :---------------------: | ------------------------------------------------------------------- |
| nio::spawn_local | !Send allowed | Pinned to current thread |
| nio::spawn_pinned | Only captured variables | Pinned to one worker thread (selected by the runtime based on load) |
| nio::spawn_pinned_at | Only captured variables | Pinned to a specific worker thread (by index) |
| nio::spawn | Send required | Not pinned, may move between threads at .await points |
Note: nio::spawn_pinned, nio::spawn_pinned_at accept async closure, Only captured variables required to be Send, task itself is !Send.
Example
[dependencies]
nio = { version = "0.1.3", features = ["tokio-io"] }
By default, Nio implements async traits from futures-io. But the optional "tokio-io" feature implements async traits from tokio::io.
Here is a basic echo server example:
use futures::AsyncWriteExt;
use nio::net::TcpListener;
use std::io::Result;
#[nio::main]
async fn main() -> Result<()> {
let mut listener = TcpListener::bind("127.0.0.1:8080").await?;
println!("{listener:#?}");
loop {
let conn = listener.accept().await?;
println!("[INCOMING] {:?}", conn.peer_addr());
let accept = || async {
// Accept the connection on different worker thread
let mut stream = conn.connect().await?;
let mut buf = vec![0; 1024];
while let Ok(n) = stream.read(&mut buf).await {
if n == 0 {
break;
}
stream.write_all(&buf[..n]).await.unwrap();
}
Result::Ok(())
};
nio::spawn_pinned(accept);
}
}
Related Skills
node-connect
347.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
