Skip to content

threadexio/channels-rs

Repository files navigation

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. It is:

  • Fast: The simple protocol allows low-overhead transporting of data.

  • Modular: Channels' sans-io approach means it can be used on top of any medium, be it a network socket, a pipe, a shared memory region, a file, anything.

  • Ergonomic: The API offered empowers you to use your time on building the logic of your application instead of worrying about data transport.

  • Async & sync first: Channels natively supports both synchronous and asynchronous operation with no hacky workarounds like spawning threads or running a separate runtime.

In action

[dependencies.channels]
version = "0.11.0"
features = ["full", "tokio"]
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/

How it works

Channels implements a communication protocol that allows sending and receiving data across any medium. It works over any stream synchronous or asynchronous. Currently it can work with any of the following IO traits:

You can find out more about how the underlying communication protocol works here.

License