Function bmrng::channel [−][src]
pub fn channel<Req, Res>(
buffer: usize
) -> (RequestSender<Req, Res>, RequestReceiver<Req, Res>)
Expand description
Creates a bounded mpsc request-response channel for communicating between asynchronous tasks with backpressure
Panics
Panics if the buffer capacity is 0, just like the Tokio MPSC channel
Examples
#[tokio::main]
async fn main() {
let buffer_size = 100;
let (tx, mut rx) = bmrng::channel::<i32, i32>(buffer_size);
tokio::spawn(async move {
while let Ok((input, mut responder)) = rx.recv().await {
if let Err(err) = responder.respond(input * input) {
println!("sender dropped the response channel");
}
}
});
for i in 1..=10 {
if let Ok(response) = tx.send_receive(i).await {
println!("Requested {}, got {}", i, response);
assert_eq!(response, i * i);
}
}
}