Trait constriction::backends::WriteWords
source · pub trait WriteWords<Word> {
type WriteError: Debug;
// Required method
fn write(&mut self, word: Word) -> Result<(), Self::WriteError>;
// Provided methods
fn extend_from_iter(
&mut self,
iter: impl Iterator<Item = Word>
) -> Result<(), Self::WriteError> { ... }
fn maybe_full(&self) -> bool { ... }
}
Expand description
A trait for sinks of compressed data (mainly used by encoders).
See the module-level documentation for more information.
Required Associated Types§
sourcetype WriteError: Debug
type WriteError: Debug
The error type that can occur when writing to the data sink, or Infallible
.
This type should be Infallible
if writing cannot fail, so that the compiler can
optimize out any error checks (see also UnwrapInfallible
).
An example error could be that the data sink is “full”, if that’s a state that the
data sink can be in. Note the asymmetry compared to ReadWords
: while we consider
an attempt to write to a “full” WriteWords
as an error, we consider an attempt to
read from an “empty” ReadWords
as a normal operation (that returns Ok(None)
).
This is because it is a common task to read all data from a data source until it is
empty (and attempting to read past the “empty” state is often the only way to detect
emptyness) whereas it is not a common task to intentionally write to a data sink
until it is full (and therefore attempting to write to a full data sink is typically
an error).
Required Methods§
sourcefn write(&mut self, word: Word) -> Result<(), Self::WriteError>
fn write(&mut self, word: Word) -> Result<(), Self::WriteError>
Writes a single Word
to the data sink and advances the state of the data sink
accordingly (i.e., so that the next write
won’t overwrite the current Word
).
Provided Methods§
sourcefn extend_from_iter(
&mut self,
iter: impl Iterator<Item = Word>
) -> Result<(), Self::WriteError>
fn extend_from_iter( &mut self, iter: impl Iterator<Item = Word> ) -> Result<(), Self::WriteError>
Writes a sequence of Word
s to the data sink, short-circuiting on error.
The default implementation calls write
for each word. You may want to overwrite
this if your data sink can perform additional optimizations (e.g., by utilizing the
provided iterator’s size_hint
).
sourcefn maybe_full(&self) -> bool
fn maybe_full(&self) -> bool
Returns true
if the data sink could be full
It is always correct to return true
from this method, even if the concept of being
“full” doesn’t apply to the data sink. The default implementation always returns
true
. The precise meaning of “full” may vary between data sinks. A data sink
that’s “not full” (i.e., where this method returns false
) may still return an
error when trying to write
to it.
Object Safety§
Implementations on Foreign Types§
source§impl<Array> WriteWords<<Array as Array>::Item> for SmallVec<Array>where
Array: Array,
impl<Array> WriteWords<<Array as Array>::Item> for SmallVec<Array>where
Array: Array,
§type WriteError = Infallible
type WriteError = Infallible
The only way how writing to a Vec<Word>
can fail is if a memory allocation fails,
which is typically treated as a fatal error (i.e., aborts) in Rust.
source§fn write(&mut self, word: Array::Item) -> Result<(), Self::WriteError>
fn write(&mut self, word: Array::Item) -> Result<(), Self::WriteError>
Appends the word to the end of the vector (= top of the stack)
fn extend_from_iter( &mut self, iter: impl Iterator<Item = Array::Item> ) -> Result<(), Self::WriteError>
fn maybe_full(&self) -> bool
source§impl<Word> WriteWords<Word> for Vec<Word>
impl<Word> WriteWords<Word> for Vec<Word>
§type WriteError = Infallible
type WriteError = Infallible
The only way how writing to a Vec<Word>
can fail is if a memory allocation fails,
which is typically treated as a fatal error (i.e., aborts) in Rust.
source§fn write(&mut self, word: Word) -> Result<(), Self::WriteError>
fn write(&mut self, word: Word) -> Result<(), Self::WriteError>
Appends the word to the end of the vector (= top of the stack)