1 个不稳定版本
0.1.1 | 2023 年 5 月 18 日 |
---|---|
0.1.0 |
|
#3 在 #recv
每月下载量 74
14KB
333 代码行(不含注释)
generic-session-types
Rust 中用于异步发送接收通道的通用会话类型
先决条件
- Cargo
- Rust nightly
rustup default nightly
用法
将依赖项添加到 Cargo.toml
[dependencies]
generic-session-types = "0.1.0"
示例
use generic_session_types::*;
type P2 = Send<String, Recv<usize, Close>>;
#[tokio::test]
async fn test_tokio_mpsc_channel2_dyn_message() {
let (client, server) = mpsc::channel::<P2, BoxAnyRepr>(10);
let msg = String::from("asdfsdfds");
let h1: JoinHandle<Result<usize, Error>> = tokio::spawn(async move {
send!(client, msg);
recv!(client, r);
close!(client);
Ok(r)
});
let h2: JoinHandle<Result<(), Error>> = tokio::spawn(async move {
let (s, c) = server.recv().await?;
let c = c.send(s.len()).await?;
c.close().await?;
Ok(())
});
let r0 = h1.await.unwrap();
let r1 = h2.await.unwrap();
assert_eq!(r0, Ok(9));
assert_eq!(r1, Ok(()));
}
- 更多示例请查看 测试
参考
依赖项
~3–10MB
~90K SLoC