Crate channels

Source
Expand description
logo

Easy and fast communication between processes, threads and systems.

license-badge tests-badge version-badge docs-badge downloads-badge


Sender/Receiver types for communicating with a channel-like API across generic IO streams. It takes the burden on serializing, deserializing and transporting data off your back and let’s you focus on the important logic of your project.

§Contents

§Examples

[dependencies.channels]
version = "0.13"
features = ["full"]
use tokio::net::TcpStream;

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
enum Message {
    Ping,
    Pong
}

#[tokio::main]
async fn main() {
    let stream = TcpStream::connect("127.0.0.1:8080").await.unwrap();
    let (r, w) = stream.into_split();
    let (mut tx, mut rx) = channels::channel::<Message, _, _>(r, w);

    loop {
        match rx.recv().await.unwrap() {
            Message::Ping => {
                println!("pinged!");
                tx.send(Message::Pong).await.unwrap();
            }
            Message::Pong => {
                println!("ponged!");
            }
        }
    }
}

For more, see: examples/

§Features

FlagDescription
aeadEncrypt and authenticate data with ring::aead.
bincodeSerialize/Deserialize data with bincode. (Enabled by default)
borshSerialize/Deserialize data with borsh.
cborSerialize/Deserialize data with ciborium.
core2Support for core2::io::{Read, Write}.
crcValidate data with a CRC checksum.
deflateCompress data with DEFLATE.
embedded-ioSupport for embedded_io::{Read, Write}.
full-ioEnable support for all of the IO traits.
full-serdesEnable features: aead, bincode, borsh, cbor, crc, deflate, hmac, json.
futuresSupport for futures::io::{AsyncRead, AsyncWrite}.
hmacAuthenticate data with a HMAC using ring::hmac.
jsonSerialize/Deserialize data with serde_json.
smolSupport for smol::io::{AsyncRead, AsyncWrite}.
statisticsCollect IO metrics such as total bytes sent/received. See: Statistics.
stdSupport for std::io::{Read, Write}. If disabled also makes the crate no_std. (Enabled by default)
tokioSupport for tokio::io::{AsyncRead, AsyncWrite}.

No two features of the crate are mutually exclusive. Instead, everything is implemented in a way to be infinitely extensible. This means, that even if you have other IO traits or another way to serialize or deserialize data, you can either add support for them directly in your own project by using the rich type system.

§How it works

Channels implements a communication protocol that allows sending and receiving data in frames. The main API of the crate is intended to work over IO traits. However, if desired, the logic of the underlying protocol is available standalone without coupling it to the usage of any IO traits. It works over any stream synchronous or asynchronous with first class support for following IO traits:

Support for each IO trait can be enabled via the corresponding feature flag. See: Features. You can read more about how the underlying communication protocol works here.

§License

Re-exports§

pub use self::receiver::Receiver;
pub use self::sender::Sender;

Modules§

error
Error types for channels.
io
Abstractions on top of synchronous and asynchronous IO interfaces.
receiver
Module containing the implementation for Receiver.
sender
Module containing the implementation for Sender.
serdes
Utilities to serialize/deserialize types.

Structs§

Statisticsstatistics
IO statistic information.

Functions§

channelbincode
Create a new channel.

Type Aliases§

Pair
A tuple containing a Sender and a Receiver.