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.
[dependencies.channels]
version = "0.12"
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/
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:
std::io::{Read, Write}
tokio::io::{AsyncRead, AsyncWrite}
futures::io::{AsyncRead, AsyncWrite}
core2::io::{Read, Write}
smol::io::{AsyncRead, AsyncWrite}
embedded_io::{Read, Write}
You can find out more about how the underlying communication protocol works here.
-
All code in this repository is licensed under the MIT license, a copy of which can be found here.
-
All artwork in this repository is licensed under Creative Commons Attribution-NonCommercial 4.0 International. A copy of the license can be found here.