Trait constriction::stream::IntoDecoder
source · pub trait IntoDecoder<const PRECISION: usize>: Encode<PRECISION> {
type IntoDecoder: Decode<PRECISION, Word = Self::Word, State = Self::State>;
// Required method
fn into_decoder(self) -> Self::IntoDecoder;
}
Expand description
A trait for permanent conversion into a matching decoder type.
This is useful for generic code that encodes some data with a user provided encoder of generic type, and then needs to obtain a compatible decoder.
See also
AsDecoder
, if you want only a temporary decoder, or if you don’t own the encoder.
Example
To be able to convert an encoder of generic type Encoder
into a decoder,
declare a trait bound Encoder: IntoDecoder<PRECISION>
. In the following
example, differences to the example for AsDecoder
are marked by // <--
.
use constriction::stream::{
model::{EncoderModel, DecoderModel, LeakyQuantizer},
stack::DefaultAnsCoder,
Decode, Encode, IntoDecoder
};
fn encode_and_decode<Encoder, D, const PRECISION: usize>(
mut encoder: Encoder, // <-- Needs ownership of `encoder`.
model: D
) -> Encoder::IntoDecoder
where
Encoder: Encode<PRECISION> + IntoDecoder<PRECISION>, // <-- Different trait bound.
D: EncoderModel<PRECISION, Symbol=i32> + DecoderModel<PRECISION, Symbol=i32> + Copy,
D::Probability: Into<Encoder::Word>,
Encoder::Word: num_traits::AsPrimitive<D::Probability>
{
encoder.encode_symbol(137, model);
let mut decoder = encoder.into_decoder();
let decoded = decoder.decode_symbol(model).unwrap();
assert_eq!(decoded, 137);
// encoder.encode_symbol(42, model); // <-- This would fail (we moved `encoder`).
decoder // <-- We can return `decoder` as it has no references to the current stack frame.
}
// Usage example:
let encoder = DefaultAnsCoder::new();
let quantizer = LeakyQuantizer::<_, _, u32, 24>::new(0..=200);
let model = quantizer.quantize(probability::distribution::Gaussian::new(0.0, 50.0));
encode_and_decode(encoder, model);
Required Associated Types§
sourcetype IntoDecoder: Decode<PRECISION, Word = Self::Word, State = Self::State>
type IntoDecoder: Decode<PRECISION, Word = Self::Word, State = Self::State>
The target type of the conversion.
Required Methods§
sourcefn into_decoder(self) -> Self::IntoDecoder
fn into_decoder(self) -> Self::IntoDecoder
Performs the conversion.
Object Safety§
This trait is not object safe.