pub trait Decoder {
type Item;
type Error: From<Error>;
// Required method
fn decode(
&mut self,
src: &mut BytesMut
) -> Result<Option<Self::Item>, Self::Error>;
}
Expand description
The Decoder
trait.
Objects that implement this trait take a BytesMut
and return
an item whose type is defined as the associated type Item
.
It is important that implementers deplete bytes from src
when
they are used to form the returned item
. This can be done
by using the split*
methods and the get*
and put*
methods
which become available when you import the Buf
and
BufMut
traits.
There are three possibilities to return from this method:
Ok(Some(Item))
- in which case decoding is complete and the user defineditem
is returned.Ok(None)
- there is not enough data insrc
to decode the message. When this is returnedread
is called again on the underlyingRead
object.Err()
- An error has occurred. This will close the underlyingRead
object. If you want to indicate a protocol error it is better to use the user definedItem
to do this.