4个版本 (稳定)
1.2.0 | 2023年12月12日 |
---|---|
1.1.0 | 2023年12月11日 |
1.0.0 | 2023年11月29日 |
0.1.0 | 2023年11月28日 |
在 并发 中排名 456
每月下载量 29
41KB
882 代码行
Either Slot
一个原子槽,发送者可以选择将其值放入槽中,或者从槽中检索所有数据。
首先,我们有主要的实现 - either
,它有2个发送者试图将它们自己的数据发送到槽中。如果其中一个成功,另一个将接收来自另一个发送者的数据,并与其自己的数据一起。如果一个发送者在另一个发送者发送之前掉线,后者将只检索自己的数据;但如果前者在后者之后掉线,后者发送的数据将被丢弃。
除了主要的实现之外,我们还将其扩展到数组槽和元组槽,分别位于 [mod@array
] 和 [mod@tuple
] 模块中。
示例
主要实现
// Both ends attempt to send their data, but only one succeeds.
use either_slot::{either, SendError};
let (a, b) = either();
a.send(1).unwrap();
assert_eq!(b.send('x'), Err(SendError::Received('x', 1)));
// both ends cannot be used any longer after access.
// let _ = (a, b);
// If one end is dropped, the other end fails to send its data and retrives
// it back.
use either_slot::{either, SendError};
let (a, b) = either::<u8, _>();
drop(a);
assert_eq!(b.send(1), Err(SendError::Disconnected(1)));
高级实现
查看 [fn@array
] 和 [fn@tuple
] 中的文档以查看相应的示例。
许可证
MIT OR Apache-2.0
依赖项
~0–25MB
~332K SLoC