Function bidirectional_channel::bounded[][src]

pub fn bounded<Req, Resp>(
    capacity: usize
) -> (Requester<Req, Resp>, Responder<ReceivedRequest<Req, Resp>>)
Expand description

Create a bounded Requester-Responder pair.
That is, once the channel is full, future senders will yield when awaiting until there’s space again

Terminology is as follows:

sequenceDiagram
    Requester ->> Responder: request
    Responder ->> Requester: response

When a Responder is asked to receive a request, it returns a ReceivedRequest The latter should be used to communicate back to the sender

use bidirectional_channel::Respond; // Don't forget to import this trait
let (requester, responder) = bidirectional_channel::bounded(10);
let future_response = requester.send(String::from("hello")).await.unwrap();

// This side of the channel receives Strings, and responds with their length
let received_request = responder.recv().await.unwrap();
let len = received_request.len(); // Deref coercion to the actual request
received_request.respond(len).unwrap();

assert!(future_response.await.unwrap() == 5);