Crate tokio_serde
source ·Expand description
This crate provides the utilities needed to easily implement a Tokio transport using serde for serialization and deserialization of frame values.
Note, if you are an end user, you probably won’t want to use this crate directly.
Instead, use a tokio-serde-*
crate that implements a specific
serialization format. For example tokio-serde-json uses serde-json to
serialize and deserialize frames.
Introduction
This crate provides transport combinators that transform a stream of frames encoded as bytes into a stream of frame values. It is expected that the framing happens at another layer. One option is to use a [length delimited] framing transport.
The crate provides two traits that must be implemented: Serializer
and
Deserializer
. Implementations of these traits are then passed to
FramedRead
or FramedWrite
along with the upsteram Stream
or
Sink
that handles the byte encoded frames.
By doing this, a transformation pipeline is built. For reading json, it looks something like this:
tokio_serde_json::JsonRead
tokio_serde::FramedRead
tokio::codec::FramedRead
tokio::net::TcpStream
The write half looks like:
tokio_serde_json::JsonWrite
tokio_serde::FramedWrite
tokio_io::codec::FramedWrite
tokio::net::TcpStream
Examples
For an example, see how tokio-serde-json is implemented: