Skip to main content

Leb128DecodeError

Struct Leb128DecodeError 

Source
pub struct Leb128DecodeError { /* private fields */ }
Expand description

Error reported while decoding a LEB128 integer from a byte buffer.

§Examples

use core::num::NonZeroUsize;
use qubit_codec_binary::Leb128DecodeError;

let error = Leb128DecodeError::incomplete(
    4,
    NonZeroUsize::new(3).expect("three is non-zero"),
    1,
);
assert_eq!(error.start_index(), 4);
assert_eq!(error.required().map(NonZeroUsize::get), Some(3));
assert_eq!(error.additional().map(NonZeroUsize::get), Some(2));

Implementations§

Source§

impl Leb128DecodeError

Source

pub const fn incomplete( start_index: usize, required: NonZeroUsize, available: usize, ) -> Self

Creates an incomplete-input decoding error.

§Parameters
  • start_index: Byte index where the incomplete value starts.
  • required: Non-zero lower bound for total bytes that must be readable from start_index before decoding can make progress.
  • available: Bytes currently available from start_index.
§Returns

Returns an error carrying incomplete-input context.

§Panics

Panics when required <= available, or when the one-past-available error boundary overflows usize.

Source

pub const fn malformed( start_index: usize, error_index: usize, consumed: NonZeroUsize, ) -> Self

Creates a malformed-input decoding error.

§Parameters
  • start_index: Byte index where the malformed value starts.
  • error_index: Byte index at which the malformed input was detected.
  • consumed: Non-zero bytes the caller may consume to make progress.
§Returns

Returns an error carrying malformed-input context.

§Panics

Panics when the consumed span end overflows usize, or when error_index is outside the consumed span.

Source

pub const fn noncanonical(start_index: usize, consumed: NonZeroUsize) -> Self

Creates a non-canonical-input decoding error.

§Parameters
  • start_index: Byte index where the non-canonical value starts.
  • consumed: Non-zero bytes the caller may consume to make progress.
§Returns

Returns an error carrying non-canonical-input context.

§Panics

Panics when the last consumed byte index overflows usize.

Source

pub const fn kind(self) -> Leb128DecodeErrorKind

Returns the decoding error kind.

§Returns

Returns Leb128DecodeErrorKind::Incomplete for an incomplete prefix, or the specific invalid-input kind for a rejected payload.

Source

pub const fn start_index(self) -> usize

Returns the absolute byte index where the attempted value starts.

§Returns

Returns the byte offset at which decoding began.

Source

pub const fn error_index(self) -> usize

Returns the absolute byte index where the error was detected.

§Returns

Returns the byte index that made the error observable. For incomplete input this is the one-past-available boundary where the next byte would be required, so it may be equal to the input length visible to the caller.

Source

pub const fn consumed(self) -> Option<NonZeroUsize>

Returns bytes that may be consumed after an invalid-input error.

§Returns

Returns Some(consumed) for invalid input, or None for incomplete input.

An unterminated over-width value may leave its tail unread after this count. Advancing by the reported count does not establish the start of the next protocol field. Recover only at an outer framing boundary or a trusted checkpoint. The codec itself does not advance a caller-owned cursor.

§Examples
use qubit_codec_binary::Leb128Codec;
use qubit_codec_binary::Leb128DecodeErrorKind;
use qubit_codec_binary::NonStrict;

let input = [0x80, 0x80, 0x01, 0x2a];
// SAFETY: The non-empty slice provides the required first readable byte.
let error = unsafe { Leb128Codec::<u8, NonStrict>::decode(&input, 0) }
    .expect_err("u8 cannot use two unterminated LEB128 bytes");
assert_eq!(Leb128DecodeErrorKind::Malformed, error.kind());
assert_eq!(Some(2), error.consumed().map(|count| count.get()));
// The unread 0x01 may still belong to the rejected over-width value.
// An outer framing boundary is needed before decoding the next field.
Source

pub const fn is_incomplete(self) -> bool

Returns whether this error reports an incomplete input prefix.

§Returns

Returns true only for an incomplete input prefix.

Source

pub const fn is_malformed(self) -> bool

Returns whether this error reports malformed input.

§Returns

Returns true only for a malformed payload.

Source

pub const fn is_noncanonical(self) -> bool

Returns whether this error reports a non-canonical representation.

§Returns

Returns true only for a non-canonical payload rejected by strict decoding.

Source

pub const fn required(self) -> Option<NonZeroUsize>

Returns a lower bound for bytes required to continue incomplete decoding.

§Returns

Returns Some(required) for incomplete input, or None otherwise. The returned value is a non-zero lower bound for the total readable bytes required from Self::start_index before decoding can make progress; it does not guarantee that the value will be complete at exactly that length.

Source

pub const fn additional(self) -> Option<NonZeroUsize>

Returns additional bytes required to continue incomplete decoding.

§Returns

Returns Some(additional) for incomplete input, or None otherwise. The value is required - available and is guaranteed to be non-zero.

Source

pub const fn available(self) -> Option<usize>

Returns bytes available for an incomplete value.

§Returns

Returns Some(available) for incomplete input, or None otherwise.

Trait Implementations§

Source§

impl Clone for Leb128DecodeError

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Leb128DecodeError

Source§

impl Debug for Leb128DecodeError

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for Leb128DecodeError

Source§

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

Formats the error with its kind, byte positions, and recovery context.

§Parameters
  • formatter: Destination formatter for the error message.
§Errors

Returns fmt::Error if writing the formatted message fails.

Source§

impl Eq for Leb128DecodeError

Source§

impl Error for Leb128DecodeError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for Leb128DecodeError

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Leb128DecodeError

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.