Trait bytecodec::Encode[][src]

pub trait Encode {
    type Item;
    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize>;
fn start_encoding(&mut self, item: Self::Item) -> Result<()>;
fn requiring_bytes(&self) -> ByteCount; fn is_idle(&self) -> bool { ... } }

This trait allows for encoding items into a byte sequence incrementally.

Associated Types

type Item[src]

The type of items to be encoded.

Loading content...

Required methods

fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize>[src]

Encodes the items in the encoder and writes the encoded bytes to the given buffer.

It returns the number of bytes written to the given buffer.

If the encoded bytes are larger than the length of buf, the encoder must consume as many bytes in the buffer as possible.

The completion of the encoding can be detected by using is_idle method.

If self.is_idle() returns false but the number of written bytes in the last encode invocation is smaller than the length of buf, it means the encoder has been suspended its work in any reasons. In that case the encoder may require some instructions from clients to resume the work, but its concrete method is beyond the scope of this trait.

The encoded bytes that could not be written to the given buffer is held by the encoder until the next invocation of the encode method.

Errors

Encoders return the following kinds of errors as necessary:

  • ErrorKind::InvalidInput:
    • An item that the encoder could not encode was passed
  • ErrorKind::UnexpectedEos:
    • The output byte stream has reached the end in the middle of an encoding process
  • ErrorKind::InconsistentState:
    • The state of the encoder bocame inconsistent
    • This means the implementation contains a bug
  • ErrorKind::Other:
    • Other errors has occurred

fn start_encoding(&mut self, item: Self::Item) -> Result<()>[src]

Tries to start encoding the given item.

If the encoder has no items to be encoded and the passed item is valid, it must accept the item.

Errors

  • ErrorKind::EncoderFull:
    • The encoder currently cannot accept any more items
  • ErrorKind::InvalidInput:
    • An invalid item was passed
  • ErrorKind::InconsistentState:
    • The state of the encoder bocame inconsistent
    • This means the implementation contains a bug
  • ErrorKind::Other:
    • Other errors has occurred

fn requiring_bytes(&self) -> ByteCount[src]

Returns the number of bytes required to encode all the items in the encoder.

If there are no items to be encoded, the encoder must return ByteCount::Finite(0).

Loading content...

Provided methods

fn is_idle(&self) -> bool[src]

Returns true if there are no items to be encoded in the encoder, otherwise false.

The default implementation returns the result of self.requiring_bytes() == ByteCount::Finite(0).

Loading content...

Implementations on Foreign Types

impl<'a, E: ?Sized + Encode> Encode for &'a mut E[src]

type Item = E::Item

impl<E: ?Sized + Encode> Encode for Box<E>[src]

type Item = E::Item

Loading content...

Implementors

impl Encode for F32beEncoder[src]

type Item = f32

impl Encode for F32leEncoder[src]

type Item = f32

impl Encode for F64beEncoder[src]

type Item = f64

impl Encode for F64leEncoder[src]

type Item = f64

impl Encode for I8Encoder[src]

type Item = i8

impl Encode for I16beEncoder[src]

type Item = i16

impl Encode for I16leEncoder[src]

type Item = i16

impl Encode for I32beEncoder[src]

type Item = i32

impl Encode for I32leEncoder[src]

type Item = i32

impl Encode for I64beEncoder[src]

type Item = i64

impl Encode for I64leEncoder[src]

type Item = i64

impl Encode for U8Encoder[src]

type Item = u8

impl Encode for U16beEncoder[src]

type Item = u16

impl Encode for U16leEncoder[src]

type Item = u16

impl Encode for U24beEncoder[src]

type Item = u32

impl Encode for U24leEncoder[src]

type Item = u32

impl Encode for U32beEncoder[src]

type Item = u32

impl Encode for U32leEncoder[src]

type Item = u32

impl Encode for U40beEncoder[src]

type Item = u64

impl Encode for U40leEncoder[src]

type Item = u64

impl Encode for U48beEncoder[src]

type Item = u64

impl Encode for U48leEncoder[src]

type Item = u64

impl Encode for U56beEncoder[src]

type Item = u64

impl Encode for U56leEncoder[src]

type Item = u64

impl Encode for U64beEncoder[src]

type Item = u64

impl Encode for U64leEncoder[src]

type Item = u64

impl Encode for NullEncoder[src]

type Item = ()

impl Encode for PaddingEncoder[src]

type Item = u8

impl<B: AsRef<[u8]>> Encode for BytesEncoder<B>[src]

type Item = B

impl<C, E, F> Encode for MapErr<C, E, F> where
    C: Encode,
    F: Fn(Error) -> E,
    Error: From<E>, 
[src]

type Item = C::Item

impl<C, T, E, F> Encode for TryMapFrom<C, T, E, F> where
    C: Encode,
    F: Fn(T) -> Result<C::Item, E>,
    Error: From<E>, 
[src]

type Item = T

impl<E0, E1> Encode for TupleEncoder<(E0, E1)> where
    E0: Encode,
    E1: Encode
[src]

type Item = (E0::Item, E1::Item)

impl<E0, E1, E2> Encode for TupleEncoder<(E0, E1, E2)> where
    E0: Encode,
    E1: Encode,
    E2: Encode
[src]

type Item = (E0::Item, E1::Item, E2::Item)

impl<E0, E1, E2, E3> Encode for TupleEncoder<(E0, E1, E2, E3)> where
    E0: Encode,
    E1: Encode,
    E2: Encode,
    E3: Encode
[src]

type Item = (E0::Item, E1::Item, E2::Item, E3::Item)

impl<E0, E1, E2, E3, E4> Encode for TupleEncoder<(E0, E1, E2, E3, E4)> where
    E0: Encode,
    E1: Encode,
    E2: Encode,
    E3: Encode,
    E4: Encode
[src]

type Item = (E0::Item, E1::Item, E2::Item, E3::Item, E4::Item)

impl<E0, E1, E2, E3, E4, E5> Encode for TupleEncoder<(E0, E1, E2, E3, E4, E5)> where
    E0: Encode,
    E1: Encode,
    E2: Encode,
    E3: Encode,
    E4: Encode,
    E5: Encode
[src]

type Item = (E0::Item, E1::Item, E2::Item, E3::Item, E4::Item, E5::Item)

impl<E0, E1, E2, E3, E4, E5, E6> Encode for TupleEncoder<(E0, E1, E2, E3, E4, E5, E6)> where
    E0: Encode,
    E1: Encode,
    E2: Encode,
    E3: Encode,
    E4: Encode,
    E5: Encode,
    E6: Encode
[src]

type Item = (E0::Item, E1::Item, E2::Item, E3::Item, E4::Item, E5::Item, E6::Item)

impl<E0, E1, E2, E3, E4, E5, E6, E7> Encode for TupleEncoder<(E0, E1, E2, E3, E4, E5, E6, E7)> where
    E0: Encode,
    E1: Encode,
    E2: Encode,
    E3: Encode,
    E4: Encode,
    E5: Encode,
    E6: Encode,
    E7: Encode
[src]

type Item = (E0::Item, E1::Item, E2::Item, E3::Item, E4::Item, E5::Item, E6::Item, E7::Item)

impl<E, I> Encode for Repeat<E, I> where
    E: Encode,
    I: Iterator<Item = E::Item>, 
[src]

type Item = I

impl<E, T, F> Encode for MapFrom<E, T, F> where
    E: Encode,
    F: Fn(T) -> E::Item
[src]

type Item = T

impl<E: MonolithicEncode> Encode for MonolithicEncoder<E>[src]

type Item = E::Item

impl<E: Encode> Encode for Last<E>[src]

type Item = Never

impl<E: Encode> Encode for Length<E>[src]

type Item = E::Item

impl<E: Encode> Encode for MaxBytes<E>[src]

type Item = E::Item

impl<E: Encode> Encode for Optional<E>[src]

type Item = Option<E::Item>

impl<E: Encode> Encode for PreEncode<E>[src]

type Item = E::Item

impl<E: Encode> Encode for Slice<E>[src]

type Item = E::Item

impl<S: AsRef<str>> Encode for Utf8Encoder<S>[src]

type Item = S

impl<T> Encode for BincodeEncoder<T> where
    T: Serialize
[src]

type Item = T

impl<T> Encode for JsonEncoder<T> where
    T: Serialize
[src]

type Item = T

Loading content...