用 Rust 来诠释 Epoll, Kqueue 和 IOCP
这其实是一本书,旨在说明 Epoll,Kqueue 和 IOCP 的工作原理,我们可以将其用于高效率、高性能的 I/O。其中一些实现将会使用 rust,原文地址:https://cfsamsonbooks.gitbook.io/epoll-kqueue-iocp-explained/
扩展阅读:Green Threads Explained in 200 Lines of Rust
reddit 上参与讨论:https://www.reddit.com/r/rust/comments/ephm4t/epoll_kqueue_and_iocp_explained_with_rust/
Deadpool
Deadpool
是一个死的简单异步池,用于任何类型的连接和对象。
Example
use async_trait::async_trait;
#[derive(Debug)]
enum Error { Fail }
struct Computer {}
struct Manager {}
type Pool = deadpool::managed::Pool<Computer, Error>;
impl Computer {
async fn get_answer(&self) -> i32 {
42
}
}
#[async_trait]
impl deadpool::managed::Manager<Computer, Error> for Manager {
async fn create(&self) -> Result<Computer, Error> {
Ok(Computer {})
}
async fn recycle(&self, conn: &mut Computer) -> deadpool::managed::RecycleResult<Error> {
Ok(())
}
}
#[tokio::main]
async fn main() {
let mgr = Manager {};
let pool = Pool::new(mgr, 16);
let mut conn = pool.get().await.unwrap();
let answer = conn.get_answer().await;
assert_eq!(answer, 42);
}
这个库还提供:Database connection pools,GitHub 地址:https://github.com/bikeshedder/deadpool
factori
Factori
is a testing factory library inspired by Ruby's FactoryBot.
Example
Factori
provides two macros: factori!, which defines a factory for a type, and create!
which instantiates it:
#[macro_use]
extern crate factori;
pub struct Vehicle {
number_wheels: u8,
electric: bool,
}
factori!(Vehicle, {
default {
number_wheels = 4,
electric = false,
}
mixin bike {
number_wheels = 2,
}
});
fn main() {
let default = create!(Vehicle);
assert_eq!(default.number_wheels, 4);
assert_eq!(default.electric, false);
// Its type is Vehicle, nothing fancy:
let vehicle: Vehicle = default;
let three_wheels = create!(Vehicle, number_wheels: 3);
assert_eq!(three_wheels.number_wheels, 3);
let electric_bike = create!(Vehicle, :bike, electric: true);
assert_eq!(electric_bike.number_wheels, 2);
assert_eq!(electric_bike.electric, true);
}
From 日报小组 @Jancd
日报订阅地址:
独立日报订阅地址:
社区学习交流平台订阅:
评论区
写评论可以 谢谢分享