RUST SCRATCH / RESEARCH #
LIBS #
connection pool: https://github.com/djc/bb8 mpmc: https://github.com/zesterer/flume and https://github.com/smol-rs/async-channel
Resources #
basic #
- The Rust Programming Language / zh_CN
- https://rustwiki.org/zh-CN/rust-by-example/
- Rust Primer
- Rust入门秘籍
- Rust first steps / zh_CN
- Rust Cookbook / zh_CN
- rustlings / Jetbrains plugin
- Learn Rust With Entirely Too Many Linked Lists
- Read Rust
- Stanford CS 110L:Safety in Systems Programming / bilibili / 2021 / 2022
- Rust course
- Rust docs / Chinese
- Visualizing memory layout of Rust’s data types
- Rust 实践指南
- 软件工艺师 / Bilibili
- Rust Language Cheat Sheet
- quickref.me/rust
- Rust API guidelines
- Programming Rust / 1st edition / ituring
- rust cn article collection
- another collection of articles on Rust and Go
Dive in #
Pin #
futures #
async/await #
Sources #
https://github.com/Hexilee/async-io-demo 关于 async 原理的一些探索
mio #
https://github.com/tokio-rs/mio 上面的文章提到了,并且这也是 tokio 在使用的 ( dependents 有 362,包括 tokio
)
metal I/O 底层的 I/O
TCP 例子。方式是poll 事件,然后逐一处理事件。docs
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!(),
}
}
}
}