Skip to main content

Codec

Struct Codec 

Source
pub struct Codec<D: StreamDeserializer, S: StreamSerializer, T> { /* private fields */ }
Available on crate feature codec only.
Expand description

Implements the codec traits of tokio-util.

The codec decodes values of type T with a StreamDeserializer and encodes values with a StreamSerializer (for instance deser_json::StreamDeserializer and deser_json::Serializer). This makes it usable with FramedRead, FramedWrite and Framed:

use futures_util::{SinkExt, StreamExt};
use deser_json::{DeserializerConfig, Serializer, SerializerConfig, StreamDeserializer, Trailing};
use deser_tokio::Codec;
use tokio_util::codec::Framed;

const READ_LINES: DeserializerConfig =
    DeserializerConfig::builder().trailing(Trailing::Newline).build();
const WRITE_LINES: SerializerConfig =
    SerializerConfig::builder().trailing(Trailing::Newline).build();

let (client, server) = tokio::io::duplex(1024);
let codec = || {
    Codec::<_, _, Vec<u32>>::new(
        StreamDeserializer::with_config(READ_LINES),
        Serializer::with_config(WRITE_LINES),
    )
};
let mut client = Framed::new(client, codec());
let mut server = Framed::new(server, codec());
client.send(vec![1, 2]).await.unwrap();
assert_eq!(server.next().await.unwrap().unwrap(), [1, 2]);

The data read by the framed reader is moved into the codec’s buffer, so errors refer to positions in the stream. If the format supports it (see StreamDeserializer::supports_partial), values are deserialized while their input arrives.

Implementations§

Source§

impl<D: StreamDeserializer, S: StreamSerializer, T> Codec<D, S, T>

Source

pub fn new(deserializer: D, serializer: S) -> Codec<D, S, T>

Creates a codec.

Source

pub fn deserializer(&self) -> &D

Returns the stream deserializer.

Source

pub fn serializer(&self) -> &S

Returns the stream serializer.

Source

pub fn set_context(&mut self, context: Context)

Sets the context the values are deserialized and serialized in.

This replaces the context of the stream deserializer (see StreamDeserializer::context). The values of the context are the defaults of the extension values of the state (see Context).

Source

pub fn context(&self) -> &Context

Returns the context the values are deserialized and serialized in.

Trait Implementations§

Source§

impl<D: StreamDeserializer, S: StreamSerializer, T: DeserializeOwned> Decoder for Codec<D, S, T>

Source§

type Item = T

The type of decoded frames.
Source§

type Error = Error

The type of unrecoverable frame decoding errors. Read more
Source§

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<T>, Error>

Attempts to decode a frame from the provided buffer of bytes. Read more
Source§

fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<T>, Error>

A default method available to be called when there are no more bytes available to be read from the underlying I/O. Read more
Source§

fn framed<T>(self, io: T) -> Framed<T, Self>
where T: AsyncRead + AsyncWrite, Self: Sized,

Provides a Stream and Sink interface for reading and writing to this Io object, using Decode and Encode to read and write the raw data. Read more
Source§

impl<D: StreamDeserializer, S: StreamSerializer, T, V: Serialize> Encoder<V> for Codec<D, S, T>

Source§

type Error = Error

The type of encoding errors. Read more
Source§

fn encode(&mut self, item: V, dst: &mut BytesMut) -> Result<(), Error>

Encodes a frame into the buffer provided. Read more

Auto Trait Implementations§

§

impl<D, S, T> !RefUnwindSafe for Codec<D, S, T>

§

impl<D, S, T> !Sync for Codec<D, S, T>

§

impl<D, S, T> !Unpin for Codec<D, S, T>

§

impl<D, S, T> !UnwindSafe for Codec<D, S, T>

§

impl<D, S, T> Freeze for Codec<D, S, T>
where InputBuffer<D>: Freeze, S: Freeze, Option<OwnedDriver<'static, T>>: Freeze, PhantomData<fn() -> T>: Freeze,

§

impl<D, S, T> Send for Codec<D, S, T>
where InputBuffer<D>: Send, S: Send, Option<OwnedDriver<'static, T>>: Send, PhantomData<fn() -> T>: Send,

§

impl<D, S, T> UnsafeUnpin for Codec<D, S, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.