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
The type of items to be encoded.
Required Methods
fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize>
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<()>
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
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)
.
Provided Methods
fn is_idle(&self) -> bool
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)
.
Implementations on Foreign Types
impl<'a, E: ?Sized + Encode> Encode for &'a mut E
[src]
impl<E: ?Sized + Encode> Encode for Box<E>
[src]
Implementors
impl<T> Encode for BincodeEncoder<T> where
T: Serialize, type Item = T;impl<B: AsRef<[u8]>> Encode for BytesEncoder<B> type Item = B;
impl<S: AsRef<str>> Encode for Utf8Encoder<S> type Item = S;
impl<C, E, F> Encode for MapErr<C, E, F> where
C: Encode,
F: Fn(Error) -> E,
Error: From<E>, type Item = C::Item;impl<E, T, F> Encode for MapFrom<E, T, F> where
E: Encode,
F: Fn(T) -> E::Item, type Item = T;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>, type Item = T;impl<E, I> Encode for Repeat<E, I> where
E: Encode,
I: Iterator<Item = E::Item>, type Item = I;impl<E: Encode> Encode for Optional<E> type Item = Option<E::Item>;
impl<E: Encode> Encode for Length<E> type Item = E::Item;
impl<E: Encode> Encode for MaxBytes<E> type Item = E::Item;
impl<E: Encode> Encode for PreEncode<E> type Item = E::Item;
impl<E: Encode> Encode for Slice<E> type Item = E::Item;
impl<E: Encode> Encode for Last<E> type Item = Never;
impl Encode for U8Encoder type Item = u8;
impl Encode for I8Encoder type Item = i8;
impl Encode for U16beEncoder type Item = u16;
impl Encode for U16leEncoder type Item = u16;
impl Encode for I16beEncoder type Item = i16;
impl Encode for I16leEncoder type Item = i16;
impl Encode for U24beEncoder type Item = u32;
impl Encode for U24leEncoder type Item = u32;
impl Encode for U32beEncoder type Item = u32;
impl Encode for U32leEncoder type Item = u32;
impl Encode for I32beEncoder type Item = i32;
impl Encode for I32leEncoder type Item = i32;
impl Encode for U40beEncoder type Item = u64;
impl Encode for U40leEncoder type Item = u64;
impl Encode for U48beEncoder type Item = u64;
impl Encode for U48leEncoder type Item = u64;
impl Encode for U56beEncoder type Item = u64;
impl Encode for U56leEncoder type Item = u64;
impl Encode for U64beEncoder type Item = u64;
impl Encode for U64leEncoder type Item = u64;
impl Encode for I64beEncoder type Item = i64;
impl Encode for I64leEncoder type Item = i64;
impl Encode for F32beEncoder type Item = f32;
impl Encode for F32leEncoder type Item = f32;
impl Encode for F64beEncoder type Item = f64;
impl Encode for F64leEncoder type Item = f64;
impl<T> Encode for JsonEncoder<T> where
T: Serialize, type Item = T;impl<E: MonolithicEncode> Encode for MonolithicEncoder<E> type Item = E::Item;
impl Encode for NullEncoder type Item = ();
impl Encode for PaddingEncoder type Item = u8;
impl<E0, E1> Encode for TupleEncoder<(E0, E1)> where
E0: Encode,
E1: Encode, type Item = (E0::Item, E1::Item);impl<E0, E1, E2> Encode for TupleEncoder<(E0, E1, E2)> where
E0: Encode,
E1: Encode,
E2: Encode, 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, 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, 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, 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, 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, type Item = (E0::Item, E1::Item, E2::Item, E3::Item, E4::Item, E5::Item, E6::Item, E7::Item);