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
impl Leb128DecodeError
Sourcepub const fn incomplete(
start_index: usize,
required: NonZeroUsize,
available: usize,
) -> Self
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 fromstart_indexbefore decoding can make progress.available: Bytes currently available fromstart_index.
§Returns
Returns an error carrying incomplete-input context.
§Panics
Panics when required <= available, or when the one-past-available
error boundary overflows usize.
Sourcepub const fn malformed(
start_index: usize,
error_index: usize,
consumed: NonZeroUsize,
) -> Self
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.
Sourcepub const fn noncanonical(start_index: usize, consumed: NonZeroUsize) -> Self
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.
Sourcepub const fn kind(self) -> Leb128DecodeErrorKind
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.
Sourcepub const fn start_index(self) -> usize
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.
Sourcepub const fn error_index(self) -> usize
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.
Sourcepub const fn consumed(self) -> Option<NonZeroUsize>
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, Leb128DecodeErrorKind, 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.Sourcepub const fn is_incomplete(self) -> bool
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.
Sourcepub const fn is_malformed(self) -> bool
pub const fn is_malformed(self) -> bool
Returns whether this error reports malformed input.
§Returns
Returns true only for a malformed payload.
Sourcepub const fn is_noncanonical(self) -> bool
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.
Sourcepub const fn required(self) -> Option<NonZeroUsize>
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.
Sourcepub const fn additional(self) -> Option<NonZeroUsize>
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.
Trait Implementations§
Source§impl Clone for Leb128DecodeError
impl Clone for Leb128DecodeError
impl Copy for Leb128DecodeError
Source§impl Debug for Leb128DecodeError
impl Debug for Leb128DecodeError
Source§impl Display for Leb128DecodeError
impl Display for Leb128DecodeError
impl Eq for Leb128DecodeError
Source§impl Error for Leb128DecodeError
impl Error for Leb128DecodeError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()