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
Feature | Description |
---|---|
statistics | Capture statistic data like: total bytes sent/received, number of send/receive operations, etc. |
tokio | Adds support for sending/receiving types asynchronously with tokio. |
futures | Adds support for sending/receiving types asynchronously with futures. |
bincode | Support for serializing/deserializing types with bincode . |
cbor | Support for serializing/deserializing types with ciborium . |
json | Support for serializing/deserializing types with serde_json . |
full | All 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§
- Error types for
channels
. - Module containing the implementation for
Receiver
. - Module containing the implementation for
Sender
. - Utilities to serialize/deserialize types.
Structs§
- Statistics
statistics
IO statistic information.
Functions§
- channel
bincode
Create a new channel.