channels

Crates.iochannels
lib.rschannels
version0.12.2
sourcesrc
created_at2022-07-23 13:23:48.954784
updated_at2024-06-20 08:35:22.048487
descriptionBidirectional channel-like communication over generic Read/Write streams.
homepagehttps://github.com/threadexio/channels-rs
repositoryhttps://github.com/threadexio/channels-rs
max_upload_size
id631405
size79,980
1337 (threadexio)

documentation

https://docs.rs/channels

README

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.12.0"
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/

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

Commit count: 429

cargo fmt