Trait bytecodec::Decode [] [src]

pub trait Decode {
    type Item;
    fn decode(
        &mut self,
        buf: &[u8],
        eos: Eos
    ) -> Result<(usize, Option<Self::Item>)>;
fn has_terminated(&self) -> bool;
fn requiring_bytes(&self) -> ByteCount; }

This trait allows for decoding items from a byte sequence incrementally.

Associated Types

The type of items to be decoded.

Required Methods

Consumes the given buffer (a part of a byte sequence), and decodes an item from it.

The first element of a succeeded result is the number of bytes consumed from buf by the decoding process.

If an item is successfully decoded, the decoder will return Ok((_, Some(..))).

If the buffer does not contain enough bytes to decode the next item, the decoder will return Ok((_, None)). In this case, the decoder must consume as many bytes in the buffer as possible.

If an item is not yet decoded but the number of consumed bytes in the last decode invocation is smaller than the length of buf, it means the decoder has been suspended its work in any reasons. In that case the decoder may require some instructions from clients to resume the work, but its concrete method is beyond the scope of this trait.

Errors

Decoders return the following kinds of errors as necessary:

  • ErrorKind::DecoderTerminated:
    • If all decodable items have been decoded, the decoder must return this kind of error when decode method is called.
  • ErrorKind::UnexpectedEos:
    • The invocation of eos.is_reached() returns true despite of the decoder requires more bytes to decode the next item.
  • ErrorKind::InvalidInput:
    • Decoded items have invalid values
    • Invalid parameters were given to decoders
  • ErrorKind::Error:
    • Other errors

Returns true if the decoder cannot decode items anymore, otherwise false.

If it returns true, the next invocation of decode method must return an ErrorKind::DecoderTerminated error, and vice versa.

Returns the lower bound of the number of bytes needed to decode the next item.

If the decoder does not know the value, it will return ByteCount::Unknown (e.g., null-terminated strings have no pre-estimable length).

If the decoder returns ByteCount::Finite(0), it means one of the followings:

  • (a) There is an already decoded item
    • The next invocation of decode() will return it without consuming any bytes
  • (b) There are no decodable items
    • All decodable items have been decoded, and the decoder cannot do any further works

Implementations on Foreign Types

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

[src]

[src]

[src]

impl<D: ?Sized + Decode> Decode for Box<D>
[src]

[src]

[src]

[src]

Implementors