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 { ... } }
Expand description
This trait allows for decoding items from a byte sequence incrementally.
Associated Types
Required methods
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.
- If all decodable items have been decoded,
the decoder must return this kind of error when
ErrorKind::UnexpectedEos
:- The invocation of
eos.is_reached()
returnstrue
despite of the decoder requires more bytes to decode the next item.
- The invocation of
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>
fn finish_decoding(&mut self) -> Result<Self::Item>
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
fn requiring_bytes(&self) -> ByteCount
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
- The next invocation of
- (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.
Provided methods
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)
.
Implementations on Foreign Types
Implementors
impl<D0, D1, D2, D3> Decode for TupleDecoder<(D0, D1, D2, D3)> where
D0: Decode,
D1: Decode,
D2: Decode,
D3: Decode,
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,
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,
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,
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,