bmrng 🪃

An async MPSC request-response channel for Tokio, where you can send a response to the sender.
Inspired by crossbeam_requests.
Example
```rust
[tokio::main]
async fn main() {
let buffersize = 100;
let (tx, mut rx) = bmrng::channel::(buffersize);
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.sendreceive(i).await {
println!("Requested {}, got {}", i, response);
asserteq!(response, i * i);
}
}
}
```
Request Timeout
It is also possible to create a channel with a request timeout:
```rust
use tokio::time::{Duration, sleep};
[tokio::main]
async fn main() {
let (tx, mut rx) = bmrng::channelwithtimeout::(100, Duration::frommillis(100));
tokio::spawn(async move {
match rx.recv().await {
Ok((input, mut responder)) => {
sleep(Duration::frommillis(200)).await;
let res = responder.respond(input * input);
asserteq!(res.isok(), true);
}
Err(err) => {
println!("all request senders dropped");
}
}
});
let response = tx.sendreceive(8).await;
asserteq!(response, Err(bmrng::error::RequestError::::RecvTimeoutError));
}
```
Unbounded Channel
There is also an unbounded alternative, bmrng::unbounded_channel()
with sync .send()
calls.