32 个版本
0.7.11 | 2020 年 12 月 29 日 |
---|---|
0.7.8 | 2020 年 9 月 28 日 |
0.7.6 | 2020 年 7 月 8 日 |
0.6.4 | 2020 年 3 月 18 日 |
#681 in 异步
每月 210 次下载
用于 2 crates
43KB
740 行
Xactor 是一个基于 async-std 的 Rust 演员(actors)框架
文档
特性
- 异步演员。
- 本地上下文中的演员通信。
- 使用 futures 进行异步消息处理。
- 类型化消息(无
Any
类型)。允许泛型消息。
示例
use xactor::*;
#[message(result = "String")]
struct ToUppercase(String);
struct MyActor;
impl Actor for MyActor {}
#[async_trait::async_trait]
impl Handler<ToUppercase> for MyActor {
async fn handle(&mut self, _ctx: &mut Context<Self>, msg: ToUppercase) -> String {
msg.0.to_uppercase()
}
}
#[xactor::main]
async fn main() -> Result<()> {
// Start actor and get its address
let mut addr = MyActor.start().await?;
// Send message `ToUppercase` to actor via addr
let res = addr.call(ToUppercase("lowercase".to_string())).await?;
assert_eq!(res, "LOWERCASE");
Ok(())
}
性能
https://github.com/sunli829/xactor-benchmarks
安装
Xactor 需要 userland 上的 async-trait。
安装了 cargo add 后,运行
$ cargo add xactor
$ cargo add async-trait
我们还提供 tokio 运行时代替 async-std。要使用它,您需要激活 runtime-tokio
并禁用默认特性。
您可以根据以下方式编辑您的 Cargo.toml
xactor = { version = "x.x.x", features = ["runtime-tokio"], default-features = false }
参考文献
依赖项
~2–13MB
~160K SLoC