Expand description
A single-producer, single-consumer “relay” channel wherein the send future doesn’t return until the receiver task has received the sent value. This can be thought of as being like a mpsc::channel with capacity 0.
Note that to use this channel at all, some form of task parallelism (eg via join! or spawn) is required.
All provided async methods are cancel-safe.
§Examples
use tokio::{join, time::Duration};
// This future will never return
async fn send_then_recv() {
let (mut tx, mut rx) = relay_channel::channel();
tx.send(42).await.unwrap();
rx.recv().await.unwrap();
}
// This future will return
async fn join_send_and_recv() {
let (mut tx, mut rx) = relay_channel::channel();
let (sent, received) = join! {
tx.send(42),
rx.recv(),
};
assert_eq!(sent, Ok(()));
assert_eq!(received, Some(42));
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let result = tokio::time::timeout(
Duration::from_millis(100),
send_then_recv()
).await;
assert!(result.is_err());
join_send_and_recv().await;
}