Crate response_channel

Source
Expand description

§Response Channel:

A wrapper crate (around the tokio::sync::mpsc channels) allowing for bidirectional communication. One can simply create a bidrectional channel by hand and manage all of the boilerplate in setting it up and handling responses, but that gets cumbersome quite quickly (especially if many bidirectional channels are required).

This crate allows the developer to create a simple bidirectional response channel using the same API as creating a tokio::sync::mpsc channel (i.e., just a simple function call).

§Example:

const BUFFER_SIZE: usize = 10;

type Message = u8;
type Response = bool;

let (mut tx, mut rx) = response_channel::channel::<Message, Response>(BUFFER_SIZE);

tokio::task::spawn(async move {
    // send the initial message and await for a response.
    let response = tx.send_await_automatic(100).await.unwrap().unwrap();
    assert!(response);
});

// receive a message and destructure it into the actual message and reverse transmission line
// (the reverse transmission line is how you send the response back to the caller!)
let (message, mut reverse_tx) = rx.recv().await.unwrap().unwrap();
let response = message >= 5;
reverse_tx.send(response).await.unwrap();

Modules§

error
The error type for this crate.

Structs§

Sender
The Sender type which contains the necessary information to provide a bidirectional response channel.

Functions§

channel
Creates a new permament, bidirectional response channel.