#broadcast-channel #broadcast #oneshot #channel #sync #send-message

oneshot-broadcast

一次广播一个值而不进行复制的 oneshot-broadcast 通道

2 个版本

0.0.2 2024年1月22日
0.0.1 2024年1月21日

#1057 in 异步

MIT/Apache

23KB
563

Crates.io Documentation

oneshot-broadcast

一次广播消息而不复制的 oneshot-broadcast 通道。

示例

use oneshot_broadcast::*;

#[tokio::main]
async fn main() {
    // Create the oneshot-broadcast channel
    let (mut sender, mut receiver) = channel::<u32>();

    // Send a message through the sender
    sender.send(10);

    // We can freely clone the receiver
    let mut receiver2 = receiver.clone();

    // and receive the message asynchronously.
    assert_eq!(receiver.recv().await.unwrap(), &10);
    assert_eq!(receiver2.recv().await.unwrap(), &10);

    // Or, if we're sure that it's ready:
    assert_eq!(receiver.get().unwrap().unwrap(), &10);

    // As an extra feature, we can erase the receiver_type by
    // turning it into a listener:
    let listener /* : Listener */ = receiver.into_listener();
    assert_eq!(listener.await, Ok(()));
}

依赖项

~85KB