Crate channels

source ·
Expand description

Channel communication across generic data streams.

§Quick start

§Async

use tokio::net::TcpStream;
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};


#[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::<i32, _, _>(r, w);

    let r = rx.recv().await.unwrap();
    println!("{r}");
    tx.send(r).await.unwrap();
}

§Sync

use std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:8080").unwrap();
let (mut tx, mut rx) = channels::channel::<i32, _, _>(stream.try_clone().unwrap(), stream);

let r = rx.recv_blocking().unwrap();
println!("{r}");
tx.send_blocking(r).unwrap();

§Examples

See: examples/

§cargo features

FeatureDescription
statisticsCapture statistic data like: total bytes sent/received, number of send/receive operations, etc.
tokioAdds support for sending/receiving types asynchronously with tokio.
futuresAdds support for sending/receiving types asynchronously with futures.
bincodeSupport for serializing/deserializing types with bincode.
cborSupport for serializing/deserializing types with ciborium.
jsonSupport for serializing/deserializing types with serde_json.
fullAll of the above except tokio and futures.

NOTE: Because of the subtle differences in the APIs of tokio and futures, it means that these 2 features must be exclusive. You cannot have both tokio and futures enabled at once.

Re-exports§

Modules§

Structs§

Functions§

  • channelbincode
    Create a new channel.

Type Aliases§