Expand description
Read and write deser values with tokio.
This crate connects the stream serializers and stream deserializers of
the data formats (which implement StreamSerializer and
StreamDeserializer, see deser::stream) to
tokio’s AsyncRead and AsyncWrite. It works with every format. Values
of formats which support it (like JSON and CBOR) are deserialized while
their input arrives, other values are buffered until they are complete,
so streams of values (like JSON Lines, CBOR sequences or YAML documents)
can be read from sockets with bounded memory:
use deser::{Deserialize, Serialize};
use deser_json::{DeserializerConfig, Serializer, SerializerConfig, StreamDeserializer, Trailing};
use deser_tokio::{Reader, Writer};
const READ_LINES: DeserializerConfig =
DeserializerConfig::builder().trailing(Trailing::Newline).build();
const WRITE_LINES: SerializerConfig =
SerializerConfig::builder().trailing(Trailing::Newline).build();
#[derive(Debug, Serialize, Deserialize)]
struct Request {
id: u64,
method: String,
}
let (input, output) = tokio::io::split(server);
// JSON Lines in, JSON Lines out
let mut requests = Reader::new(input, StreamDeserializer::with_config(READ_LINES));
let mut responses = Writer::new(output, Serializer::with_config(WRITE_LINES));
while let Some(request) = requests.read::<Request>().await? {
responses.write(&request.id).await?;
}Single values are read with from_reader and written with
to_writer. With the codec feature, Codec implements the codec
traits of tokio-util for use with
FramedRead, FramedWrite and Framed.
§Multi-Threaded Runtimes
The futures are Send if the reader or writer and the stream
deserializer or serializer are, so they can be spawned on multi-threaded
runtimes.
§Large Values
Formats which support it (like JSON and CBOR) serialize values in parts: once the output of a value exceeds the buffer limit, what was serialized so far is written and the serialization continues after that. The memory used for writing does not depend on the size of the values either.
§Cancellation
Reading is cancellation safe: if a future that reads a value is
dropped, the data read so far stays in the buffer of the Reader and
the next read continues with it. This allows reading in
tokio::select!. Writing is not cancellation safe, a value might have
been written partially. A Writer refuses to write more values after
a value was abandoned after a part of it was written.
Structs§
- Codec
codec - Implements the codec traits of
tokio-util. - Element
Stream - A
Streamof the elements of theStreamedsequence of values and the values. - Reader
- Reads values from an
AsyncRead. - Reader
Stream - A
Streamof the values of aReader. - Writer
- Writes values to an
AsyncWrite.
Functions§
- from_
reader - Reads a single value from an
AsyncRead. - to_
writer - Writes a single value to an
AsyncWriteand flushes it.