Trait bytecodec::Decode[][src]

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

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

Associated Types

type Item[src]

The type of items to be decoded.

Loading content...

Required methods

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

Consumes the given buffer (a part of a byte sequence), and proceeds the decoding process.

It returns the number of bytes consumed from the input buffer.

If an item is completely decoded, the next invocation of is_idle method will return true. And if is_idle method returns true, decode method should consume no bytes.

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

The following errors may be returned by the decoder:

  • 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::InconsistentState:
    • The state of the decoder bocame inconsistent
    • This means the implementation contains a bug
  • ErrorKind::Other:
    • Other errors

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

Finishes the current decoding process and returns the decoded item.

Errors

The following errors may be returned by the decoder:

  • ErrorKind::IncompleteDecoding:
    • The decoding process has not been completed
  • ErrorKind::DecoderTerminated:
    • The decoder has terminated (i.e., cannot decode any more items)
  • ErrorKind::InconsistentState:
    • The state of the decoder bocame inconsistent
    • This means the implementation contains a bug
  • ErrorKind::Other:
    • Other errors

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

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
    • In this case, the next invocation of decode method will fail.
Loading content...

Provided methods

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

Returns true if there are no items to be decoded by the decoder at the next invocation of decode method, otherwise false.

Typically, true means the decoder already has a decoded item and it is waiting for finish_decoding to be called.

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

Loading content...

Implementations on Foreign Types

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

type Item = D::Item

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

type Item = D::Item

Loading content...

Implementors

impl Decode for RemainingBytesDecoder[src]

type Item = Vec<u8>

impl Decode for F32beDecoder[src]

type Item = f32

impl Decode for F32leDecoder[src]

type Item = f32

impl Decode for F64beDecoder[src]

type Item = f64

impl Decode for F64leDecoder[src]

type Item = f64

impl Decode for I8Decoder[src]

type Item = i8

impl Decode for I16beDecoder[src]

type Item = i16

impl Decode for I16leDecoder[src]

type Item = i16

impl Decode for I32beDecoder[src]

type Item = i32

impl Decode for I32leDecoder[src]

type Item = i32

impl Decode for I64beDecoder[src]

type Item = i64

impl Decode for I64leDecoder[src]

type Item = i64

impl Decode for U8Decoder[src]

type Item = u8

impl Decode for U16beDecoder[src]

type Item = u16

impl Decode for U16leDecoder[src]

type Item = u16

impl Decode for U24beDecoder[src]

type Item = u32

impl Decode for U24leDecoder[src]

type Item = u32

impl Decode for U32beDecoder[src]

type Item = u32

impl Decode for U32leDecoder[src]

type Item = u32

impl Decode for U40beDecoder[src]

type Item = u64

impl Decode for U40leDecoder[src]

type Item = u64

impl Decode for U48beDecoder[src]

type Item = u64

impl Decode for U48leDecoder[src]

type Item = u64

impl Decode for U56beDecoder[src]

type Item = u64

impl Decode for U56leDecoder[src]

type Item = u64

impl Decode for U64beDecoder[src]

type Item = u64

impl Decode for U64leDecoder[src]

type Item = u64

impl Decode for NullDecoder[src]

type Item = ()

impl Decode for PaddingDecoder[src]

type Item = ()

impl<B: AsRef<[u8]> + AsMut<[u8]> + Copy> Decode for CopyableBytesDecoder<B>[src]

type Item = B

impl<B: AsRef<[u8]> + AsMut<[u8]>> Decode for BytesDecoder<B>[src]

type Item = B

impl<D0, D1> Decode for TupleDecoder<(D0, D1)> where
    D0: Decode,
    D1: Decode
[src]

type Item = (D0::Item, D1::Item)

impl<D0, D1, D2> Decode for TupleDecoder<(D0, D1, D2)> where
    D0: Decode,
    D1: Decode,
    D2: Decode
[src]

type Item = (D0::Item, D1::Item, D2::Item)

impl<D0, D1, D2, D3> Decode for TupleDecoder<(D0, D1, D2, D3)> where
    D0: Decode,
    D1: Decode,
    D2: Decode,
    D3: Decode
[src]

type Item = (D0::Item, D1::Item, D2::Item, D3::Item)

impl<D0, D1, D2, D3, D4> Decode for TupleDecoder<(D0, D1, D2, D3, D4)> where
    D0: Decode,
    D1: Decode,
    D2: Decode,
    D3: Decode,
    D4: Decode
[src]

type Item = (D0::Item, D1::Item, D2::Item, D3::Item, D4::Item)

impl<D0, D1, D2, D3, D4, D5> Decode for TupleDecoder<(D0, D1, D2, D3, D4, D5)> where
    D0: Decode,
    D1: Decode,
    D2: Decode,
    D3: Decode,
    D4: Decode,
    D5: Decode
[src]

type Item = (D0::Item, D1::Item, D2::Item, D3::Item, D4::Item, D5::Item)

impl<D0, D1, D2, D3, D4, D5, D6> Decode for TupleDecoder<(D0, D1, D2, D3, D4, D5, D6)> where
    D0: Decode,
    D1: Decode,
    D2: Decode,
    D3: Decode,
    D4: Decode,
    D5: Decode,
    D6: Decode
[src]

type Item = (D0::Item, D1::Item, D2::Item, D3::Item, D4::Item, D5::Item, D6::Item)

impl<D0, D1, D2, D3, D4, D5, D6, D7> Decode for TupleDecoder<(D0, D1, D2, D3, D4, D5, D6, D7)> where
    D0: Decode,
    D1: Decode,
    D2: Decode,
    D3: Decode,
    D4: Decode,
    D5: Decode,
    D6: Decode,
    D7: Decode
[src]

type Item = (D0::Item, D1::Item, D2::Item, D3::Item, D4::Item, D5::Item, D6::Item, D7::Item)

impl<D0, D1, F> Decode for AndThen<D0, D1, F> where
    D0: Decode,
    D1: Decode,
    F: Fn(D0::Item) -> D1, 
[src]

type Item = D1::Item

impl<D> Decode for Utf8Decoder<D> where
    D: Decode<Item = Vec<u8>>, 
[src]

type Item = String

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

type Item = D::Item

impl<D, T> Decode for CollectN<D, T> where
    D: Decode,
    T: Default + Extend<D::Item>, 
[src]

type Item = T

impl<D, T, E, F> Decode for TryMap<D, T, E, F> where
    D: Decode,
    F: Fn(D::Item) -> Result<T, E>,
    Error: From<E>, 
[src]

type Item = T

impl<D, T, F> Decode for Map<D, T, F> where
    D: Decode,
    F: Fn(D::Item) -> T, 
[src]

type Item = T

impl<D, T: Default> Decode for Collect<D, T> where
    D: Decode,
    T: Extend<D::Item>, 
[src]

type Item = T

impl<D: MonolithicDecode> Decode for MonolithicDecoder<D>[src]

type Item = D::Item

impl<D: Decode> Decode for Length<D>[src]

type Item = D::Item

impl<D: Decode> Decode for MaxBytes<D>[src]

type Item = D::Item

impl<D: Decode> Decode for MaybeEos<D>[src]

type Item = D::Item

impl<D: Decode> Decode for Omittable<D>[src]

type Item = Option<D::Item>

impl<D: Decode> Decode for Peekable<D>[src]

type Item = D::Item

impl<D: Decode> Decode for Slice<D>[src]

type Item = D::Item

impl<T> Decode for BincodeDecoder<T> where
    T: for<'de> Deserialize<'de>, 
[src]

type Item = T

impl<T> Decode for JsonDecoder<T> where
    T: for<'de> Deserialize<'de>, 
[src]

type Item = T

Loading content...