Skip to main content

CodecValueDecoder

Struct CodecValueDecoder 

Source
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,

Source

pub fn new(codec: C) -> Self

Creates a decoder backed by codec.

§Parameters
  • codec: Low-level codec used to decode one value.
§Returns

Returns a value decoder adapter for the supplied codec.

§Panics

Fails at compile time if codec decode bounds are zero or the minimum exceeds the maximum.

Source

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.

Source

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.

Source

pub fn into_codec(self) -> C

Consumes the adapter and returns its wrapped codec.

§Returns

The owned codec, preserving its most recent lifecycle state.

Source

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.

Source

pub fn decode_lifecycle( &mut self, input: &[C::Unit], ) -> Result<DecodeLifecycleOutput<C::Value>, TranscodeDecodeErrorOf<C>>
where C::Value: Default,

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.

Source

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>
where C: Codec + Debug,

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the decoder without requiring finished values to be printable.

§Parameters
  • formatter: Destination and formatting options.
§Returns

Unit on successful formatting.

§Errors

Returns the formatter error if writing the codec representation fails.

Source§

impl<C> Default for CodecValueDecoder<C>
where C: Codec + Default,

Source§

fn default() -> Self

Creates a decoder from the default codec.

§Returns

An adapter owning C::default().

§Panics

Fails at compile time for invalid codec decode bounds, as Self::new. May panic if the codec’s own default constructor panics.

Source§

impl<C> ValueDecoder<[<C as Codec>::Unit]> for CodecValueDecoder<C>
where C: Codec,

Source§

type Output = <C as Codec>::Value

Owned value returned after the complete decode lifecycle.

Source§

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>

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.

Auto Trait Implementations§

§

impl<C> Freeze for CodecValueDecoder<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for CodecValueDecoder<C>
where C: RefUnwindSafe,

§

impl<C> Send for CodecValueDecoder<C>
where C: Send,

§

impl<C> Sync for CodecValueDecoder<C>
where C: Sync,

§

impl<C> Unpin for CodecValueDecoder<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for CodecValueDecoder<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for CodecValueDecoder<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.