pub struct CodecValueDecoder<C>where
C: Codec,{ /* private fields */ }Expand description
Decodes one encoded unit slice into one owned value by using a Codec.
CodecValueDecoder is the default bridge from the low-level unchecked
Codec contract to the convenience-layer ValueDecoder contract. The
supplied input slice must contain exactly one encoded value. The adapter
runs the complete decode lifecycle, including Codec::decode_finish,
before returning the decoded value.
§Type Parameters
C: Low-level codec used to decode one value.
§Examples
use std::convert::Infallible;
use std::num::NonZeroUsize;
use qubit_codec::Codec;
use qubit_codec::CodecValueDecoder;
use qubit_codec::DecodeFailure;
struct Identity;
impl Codec for Identity {
type Value = u8;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = 1;
const MAX_DECODE_UNITS_PER_VALUE: usize = 1;
const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;
unsafe fn decode(&mut self, input: &[u8], index: usize)
-> Result<(u8, NonZeroUsize), DecodeFailure<Infallible>> {
Ok((input[index], NonZeroUsize::new(1).unwrap()))
}
unsafe fn encode(&mut self, value: &u8, output: &mut [u8], index: usize)
-> Result<usize, Infallible> {
output[index] = *value;
Ok(1)
}
}
let mut decoder = CodecValueDecoder::new(Identity);
assert_eq!(decoder.decode(&[7]).unwrap(), 7);Implementations§
Source§impl<C> CodecValueDecoder<C>where
C: Codec,
impl<C> CodecValueDecoder<C>where
C: Codec,
Sourcepub const fn codec(&self) -> &C
pub const fn codec(&self) -> &C
Returns a shared reference to the wrapped codec.
§Returns
The codec borrowed for as long as the adapter is borrowed.
Sourcepub fn codec_mut(&mut self) -> &mut C
pub fn codec_mut(&mut self) -> &mut C
Returns a mutable reference to the wrapped codec.
The next value operation starts a fresh codec lifecycle, so mutations are applied to the following operation.
§Returns
An exclusive borrow of the codec without allocating.
Sourcepub fn into_codec(self) -> C
pub fn into_codec(self) -> C
Consumes the adapter and returns its wrapped codec.
§Returns
The owned codec, preserving its most recent lifecycle state.
Sourcepub fn decode(
&mut self,
input: &[C::Unit],
) -> Result<C::Value, TranscodeDecodeErrorOf<C>>
pub fn decode( &mut self, input: &[C::Unit], ) -> Result<C::Value, TranscodeDecodeErrorOf<C>>
Decodes exactly one encoded value from input.
§Parameters
input: Encoded units for exactly one value.
§Returns
Returns the decoded value.
Codecs that may emit decode reset or finish values must use
Self::decode_lifecycle or Self::decode_lifecycle_with_scratch.
§Errors
Returns crate::TranscodeFailure::UnsupportedDecodeLifecycleOutput
before inspecting input or running codec hooks when reset or finish
may emit values. Returns
crate::TranscodeFailure::IncompleteInput when fewer than
Codec::MIN_UNITS_PER_VALUE units are available or when
crate::DecodeFailure::Incomplete is reported by the codec. In this
one-shot API, incomplete input is a terminal error rather than a
resumable streaming status. Returns a domain error when the wrapped
codec rejects or cannot finish the input. Returns
crate::TranscodeFailure::TrailingInput when a value is decoded but
extra input remains.
§Panics
Panics when the wrapped codec reports a consumed unit count larger than
the input slice length or Codec::MAX_DECODE_UNITS_PER_VALUE.
Sourcepub fn decode_lifecycle(
&mut self,
input: &[C::Unit],
) -> Result<DecodeLifecycleOutput<C::Value>, TranscodeDecodeErrorOf<C>>
pub fn decode_lifecycle( &mut self, input: &[C::Unit], ) -> Result<DecodeLifecycleOutput<C::Value>, TranscodeDecodeErrorOf<C>>
Decodes exactly one value and preserves all lifecycle output.
§Parameters
input: Encoded units for exactly one value.
§Returns
Returns reset values, the main decoded value, and finish values in separate owned buffers.
§Errors
Returns a framework error when input is incomplete or has trailing units. Returns a phase-aware domain error when reset, decode, or finish fails. Returns an allocation failure when either lifecycle buffer cannot reserve its declared capacity.
§Panics
Panics when the wrapped codec violates its declared reset, decode, or finish bounds.
Sourcepub fn decode_lifecycle_with_scratch(
&mut self,
input: &[C::Unit],
reset_output: &mut [C::Value],
finish_output: &mut [C::Value],
) -> Result<DecodeLifecycleProgress<C::Value>, TranscodeDecodeErrorOf<C>>
pub fn decode_lifecycle_with_scratch( &mut self, input: &[C::Unit], reset_output: &mut [C::Value], finish_output: &mut [C::Value], ) -> Result<DecodeLifecycleProgress<C::Value>, TranscodeDecodeErrorOf<C>>
Decodes exactly one value into separate lifecycle output buffers.
§Parameters
input: Encoded units for exactly one value.reset_output: Destination for values emitted by decode reset.finish_output: Destination for values emitted by decode finish.
§Returns
Returns the main decoded value and the initialized lengths of both lifecycle output buffers.
§Errors
Returns a framework error before running any lifecycle hook when either output buffer is shorter than its corresponding codec bound. Also returns incomplete-input, trailing-input, or phase-aware domain errors.
§Panics
Panics when the wrapped codec violates its declared reset, decode, or finish bounds.
Trait Implementations§
Source§impl<C> Debug for CodecValueDecoder<C>
impl<C> Debug for CodecValueDecoder<C>
Source§impl<C> Default for CodecValueDecoder<C>
impl<C> Default for CodecValueDecoder<C>
Source§impl<C> ValueDecoder<[<C as Codec>::Unit]> for CodecValueDecoder<C>where
C: Codec,
impl<C> ValueDecoder<[<C as Codec>::Unit]> for CodecValueDecoder<C>where
C: Codec,
Source§type Error = TranscodeDecodeError<<C as Codec>::DecodeError>
type Error = TranscodeDecodeError<<C as Codec>::DecodeError>
Framework or phase-aware codec failure from one-value decoding.
Source§fn decode(&mut self, input: &[C::Unit]) -> Result<Self::Output, Self::Error>
fn decode(&mut self, input: &[C::Unit]) -> Result<Self::Output, Self::Error>
Decodes one value through the owned-value adapter.
§Parameters
input: Complete encoded representation of exactly one value.
§Returns
The owned decoded value after finishing the codec lifecycle.
§Errors
Returns the same unsupported-lifecycle, incomplete-input,
trailing-input, and codec errors as Self::decode.
§Panics
Panics if the codec reports consumption beyond its bounds or the input.