Crates.io | oneshot-broadcast |
lib.rs | oneshot-broadcast |
version | 0.0.2 |
source | src |
created_at | 2024-01-21 22:59:42.663312 |
updated_at | 2024-01-22 00:10:42.648504 |
description | A oneshot-broadcast channel that broadcasts a value once without cloning it. |
homepage | https://github.com/jvdwrf/oneshot-broadcast |
repository | https://github.com/jvdwrf/oneshot-broadcast |
max_upload_size | |
id | 1107944 |
size | 22,962 |
A oneshot-broadcast channel that broadcasts a value once without cloning the message.
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(()));
}