Crate tchannel [] [src]

Optimized channels for Rust. This crate contains verious version of the same concept, channels. The available versions are:

  • oneshot, allows a single value to be send and received.
  • mpsc, allows for multiple producers (senders) and a single consumer (receiver).

All these different versions work in the same way. All modules will have a channel function which returns a Sender and a Receiver. The Sender can only send values and the Receiver can only receive values. The remaing API are errors, which are a bit more channel version specific, and adapters for the futures crate, see below.

Example

We'll use the oneshot channel for this example, but all versions will have roughly the same type and structure setup.

use tchannel::oneshot::{channel, Sender, Receiver, ReceiveError};

// Create a new channel, which has a sender and a receiver.
let (sender, receiver): (Sender<_>, Receiver<_>) = channel();

// First we'll try to receive a value, but the channel should be empty so
// it will return an error. We can recover the receiver (`try_receive`
// consumes `Receiver` in case of an oneshot channel) via the `Empty` error.
let receiver = match receiver.try_receive() {
    Err(ReceiveError::Empty(receiver)) => receiver,
    _ => panic!("expected the channel to be empty"),
};

// Now we'll send our value along the channel. In case of a oneshot channel
// this will consume the `Sender`.
//
// This will only return an error if the receiving side of the channel is
// disconnected, in which case it will return a `SendError` which allows
// the send value to retrieved.
assert_eq!(sender.send("Hello world"), Ok(()));

// Here we'll receive the value, making sure it's the same as what we send.
assert_eq!(receiver.try_receive().unwrap(), "Hello world");

Futures

Support for the futures crate is available if the with-futures feature is enabled (disabled by default).

TODO: add example usage.

Modules

mpsc

Multiple producers, single consumer channels.

oneshot

Oneshot channels.

Structs

SendError

This error will be returned if the receiving side of the channel is disconnected, while trying to send a value across the channel.

Enums

ReceiveError

The error returned by trying to receive a value.