Trait push_decode::Encoder
source · pub trait Encoder: Sized {
// Required methods
fn encoded_chunk(&self) -> &[u8] ⓘ;
fn next(&mut self) -> bool;
// Provided methods
fn track_position(self) -> EncoderPositionTracker<Self> { ... }
fn write_to_vec(self, buf: &mut Vec<u8>) { ... }
fn write_all_sync<W: Write + BufWrite>(self, writer: W) -> Result<()> { ... }
fn write_all_sync_lgio<W: BufWrite>(
self,
writer: W
) -> Result<(), W::WriteError> { ... }
fn write_all_tokio<W: AsyncWrite + BufWrite>(
self,
writer: W
) -> TokioEncodeFuture<W, Self> ⓘ { ... }
fn write_all_async_std<W: Write + BufWrite>(
self,
writer: W
) -> AsyncStdEncodeFuture<W, Self> ⓘ { ... }
fn write_all_futures_0_3<W: AsyncWrite + BufWrite>(
self,
writer: W
) -> Futures0Dot3EncodeFuture<W, Self> ⓘ { ... }
fn then<E: Encoder, F: FnMut() -> E>(
self,
second_encoder_constructor: F
) -> Then<Self, E, F> { ... }
fn chain<T: Encoder>(self, second_encoder: T) -> Chain<Self, T> { ... }
}Expand description
Represents types producing bytes of some encoded value.
Required Methods§
sourcefn encoded_chunk(&self) -> &[u8] ⓘ
fn encoded_chunk(&self) -> &[u8] ⓘ
Provides next chunk of encoded bytes.
The returned bytes represent a part of the value being encoded and should be written to a writer by the consumer. Empty returned value indicates end - there are no more bytes to be encoded.
The returned value MUST be the same for all calls of encoded_chunk until
next() is called. IOW it’s not allowed to use interior mutability or global
state (randomness) to affect the returned value.
It’s recommended that this method returns bytes within the value if possible or minimal required buffer otherwise. The consumers of the trait are responsible for buffering, so buffering inside encoder would decrease performance.
sourcefn next(&mut self) -> bool
fn next(&mut self) -> bool
Advances the state to get the next chunk of encoded bytes.
Calling this method signals to the encoder that the current chunk was fully processed. The encoder MUST either:
- return
trueand provide the next chunk of data in folowing calls toencoded_chunk. - return
falseindicating there is no more data.
The encoder MUST NOT panic or cause similar undesirable behavior if next was called again
after it previously returned false. It must simply return false again.
Note that the implementor DOES NOT need to guarantee that encoded_chunk is empty after
this method returned false and for performance reasons it’s not advisable to do in
release builds. The consumers are responsible for handling this situation. The consumers
may use track_position to get a more convenient interface handling
these edge-cases and tracking byte position.
Provided Methods§
sourcefn track_position(self) -> EncoderPositionTracker<Self>
fn track_position(self) -> EncoderPositionTracker<Self>
Returns a wrapper that tracks the position of processed bytes.
The returned wrapper has a bit different interface that is more suitable for writing into writers. It can handle partial writes and makes handling of end easier. Thus downstream consumers are encouraged to use it where it makes sense.
sourcefn write_to_vec(self, buf: &mut Vec<u8>)
fn write_to_vec(self, buf: &mut Vec<u8>)
alloc only.Writes all encoded bytes to a vec.
Note that this does not call reserve since there’s no way to know the amount to
reserve. This should be handled by the code producing the decoder instead.
sourcefn write_all_sync<W: Write + BufWrite>(self, writer: W) -> Result<()>
fn write_all_sync<W: Write + BufWrite>(self, writer: W) -> Result<()>
std only.Writes all encoded bytes to the std writer.
sourcefn write_all_sync_lgio<W: BufWrite>(
self,
writer: W
) -> Result<(), W::WriteError>
fn write_all_sync_lgio<W: BufWrite>( self, writer: W ) -> Result<(), W::WriteError>
lgio only.Writes all encoded bytes to the std writer.
sourcefn write_all_tokio<W: AsyncWrite + BufWrite>(
self,
writer: W
) -> TokioEncodeFuture<W, Self> ⓘ
fn write_all_tokio<W: AsyncWrite + BufWrite>( self, writer: W ) -> TokioEncodeFuture<W, Self> ⓘ
tokio only.Writes all encoded bytes to the tokio async writer.
The returned future resolves to std::io::Result<()>.
sourcefn write_all_async_std<W: Write + BufWrite>(
self,
writer: W
) -> AsyncStdEncodeFuture<W, Self> ⓘ
fn write_all_async_std<W: Write + BufWrite>( self, writer: W ) -> AsyncStdEncodeFuture<W, Self> ⓘ
async-std only.Writes all encoded bytes to the async-std async writer.
The returned future resolves to std::io::Result<()>.
sourcefn write_all_futures_0_3<W: AsyncWrite + BufWrite>(
self,
writer: W
) -> Futures0Dot3EncodeFuture<W, Self> ⓘ
fn write_all_futures_0_3<W: AsyncWrite + BufWrite>( self, writer: W ) -> Futures0Dot3EncodeFuture<W, Self> ⓘ
futures_0_3 only.Writes all encoded bytes to the futures 0.3 async writer.
The returned future resolves to std::io::Result<()>.
sourcefn then<E: Encoder, F: FnMut() -> E>(
self,
second_encoder_constructor: F
) -> Then<Self, E, F>
fn then<E: Encoder, F: FnMut() -> E>( self, second_encoder_constructor: F ) -> Then<Self, E, F>
Chains an encoder constructed by second_encoder_constructor after this one.
This is similar to chain but only incurs the cost of creating the encoder
if it’s actually needed. So if encoding stops before finishing (e.g. due to error) no CPU
time or memory is wasted.
This will also save memory if the second encoder is larger than F.
Note: F needs to be FnMut instead of FnOnce to correctly handle panics.