Trait constriction::stream::Code
source · pub trait Code {
type Word: BitArray;
type State: Clone;
// Required method
fn state(&self) -> Self::State;
// Provided methods
fn encoder_maybe_full<const PRECISION: usize>(&self) -> bool
where Self: Encode<PRECISION> { ... }
fn decoder_maybe_exhausted<const PRECISION: usize>(&self) -> bool
where Self: Decode<PRECISION> { ... }
}
Expand description
Base trait for stream encoders and decoders
This trait has to be implemented by all stream encoders and decoders. In addition,
you’ll likely want to implement at least one of the Encode
and Decode
trait.
While some stream coders may implement both Encode
and Decode
for the same type
(e.g., AnsCoder
and ChainCoder
if the default backends are used), others, like
the RangeEncoder
and RangeDecoder
provide different types that specialize for
encoding or decoding only.
This trait defines the Word
and State
types, which
apply to both encoding and decoding.
Naming Convention
This trait is deliberately called Code
(as in the verb “to code”) and not Coder
so
that the term Coder
can still be used for generic type parameters without leading to
confusion with trait bounds. For example, you may want to write impl Wrapper<Coder> where Coder: Code
. Using the verb for trait names and the noun for types has precedence
in the standard library: see, e.g., the BufRead
trait, which is implemented by the
BufReader
type.
Required Associated Types§
sourcetype Word: BitArray
type Word: BitArray
The smallest unit of compressed data that this coder can emit or read at once. Most
coders guarantee that encoding emits at most one Word
per symbol (plus a constant
overhead).
sourcetype State: Clone
type State: Clone
The internal coder state, as returned by the method state
.
This internal coder state is relevant if the coder implements Pos
and/or
Seek
. By convention, Pos::pos
returns a tuple (pos, state)
where pos
represents the position in the backend(s) while state
is the internal coder state.
Most implementations of Code
are generic over a type parameter State
. Note that,
while the associated type Code::State
is typically related to the type parameter
State
, they do not necessarily need to be the same. For example, in a
RangeEncoder
, the associated type <RangeEncoder as Code>::State
is a struct of
type RangeCoderState
, which has twice the size of the type parameter State
(same for RangeDecoder
).
Required Methods§
Provided Methods§
sourcefn encoder_maybe_full<const PRECISION: usize>(&self) -> boolwhere
Self: Encode<PRECISION>,
fn encoder_maybe_full<const PRECISION: usize>(&self) -> boolwhere
Self: Encode<PRECISION>,
Checks if there might not be any room to encode more data.
This is a convenience forwarding method to simplify type annotations. In the default
implementation, calling encoder.encoder_maybe_full::<PRECISION>()
is equivalent to
Encode::<PRECISION>::maybe_full(&encoder)
. See Encode::maybe_full.
sourcefn decoder_maybe_exhausted<const PRECISION: usize>(&self) -> boolwhere
Self: Decode<PRECISION>,
fn decoder_maybe_exhausted<const PRECISION: usize>(&self) -> boolwhere
Self: Decode<PRECISION>,
Checks if there might be no compressed data left for decoding.
This is a convenience forwarding method to simplify type annotations. In the default
implementation, calling decoder.decoder_maybe_exhausted::<PRECISION>()
is
equivalent to Decode::<PRECISION>::maybe_exhausted(&decoder)
. See
Decode::maybe_exhausted.