Crate async_oneshot_channel

Crate async_oneshot_channel 

Source
Expand description

A simple (<100 LoC) “oneshot” channel for asynchronously sending a single value between tasks, in a thread-safe manner and async-runtime-agnostic manner.

This crate provides a oneshot channel that allows sending a single value from one producer to one consumer. The handle to the sender can be cloned, but only one send operation succeeds. Supports tasks running on different threads.

See oneshot for more details.

§Examples

Basic usage:

use async_oneshot_channel::oneshot;
let (tx, rx) = oneshot();
let result = tx.send(42);
assert!(result.is_ok());

let received = block_on(rx.recv());
assert_eq!(received, Some(42));

Multiple senders (only one succeeds):

let (tx1, rx) = oneshot();
let tx2 = tx1.clone();

// First send succeeds
assert!(tx1.send(1).is_ok());
// Second send fails and returns the value
assert_eq!(tx2.send(2), Err(2));

let received = block_on(rx.recv());
assert_eq!(received, Some(1));

Handling sender drop:

let (tx, rx) = oneshot::<()>();
drop(tx);

// Receiver gets None when all senders are dropped without sending
let received = block_on(rx.recv());
assert_eq!(received, None);

Structs§

Receiver
The receiving half of the oneshot channel.
Recv
Future returned by oneshot. When awaited, resolves to the value sent through the channel, or None if either:
Sender
The sending half of the oneshot channel.
WeakSender

Functions§

oneshot
Creates a new oneshot channel pair of sender and receiver.