Skip to main content

Error

Enum Error 

Source
#[non_exhaustive]
pub enum Error { UnsignedVarintDecode { source: Error, }, InsufficientData { expected: usize, actual: usize, }, InvalidEncoding { reason: String, }, }
Expand description

Errors generated by the numeric type impls

This error type follows Rust error handling best practices:

  • Uses #[non_exhaustive] to allow adding new variants without breaking changes
  • Provides structured error variants with context information
  • Implements proper error source chains via #[source] attribute
  • Uses thiserror for ergonomic error handling

§Error Source Chains

Errors that wrap other errors (like UnsignedVarintDecode) properly implement the Error::source() method, allowing error chains to be inspected for debugging.

§Examples

use multi_trait::{TryDecodeFrom, Error};

// Attempting to decode from empty slice returns an error
let result = u8::try_decode_from(&[]);
assert!(result.is_err());

// The error provides context about what went wrong
if let Err(e) = result {
    eprintln!("Decode failed: {}", e);
    // In production code with std, you can access the error source:
    // if let Some(source) = std::error::Error::source(&e) {
    //     eprintln!("Caused by: {}", source);
    // }
}

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

UnsignedVarintDecode

Failed to decode unsigned varint data

This error occurs when the underlying unsigned-varint decoder fails. Common causes include:

  • Truncated varint data (incomplete bytes)
  • Invalid varint encoding
  • Buffer underflow

The source error provides additional details about the specific failure.

Fields

§source: Error

The underlying decode error from unsigned-varint crate

§

InsufficientData

Insufficient data to decode value

This error occurs when the input slice doesn’t contain enough bytes to decode the requested type. This typically happens with truncated data.

§Examples

use multi_trait::{TryDecodeFrom, Error};

// Empty slice cannot decode any value
let result = u16::try_decode_from(&[]);
match result {
    Err(Error::UnsignedVarintDecode { .. }) => {
        // Expected behavior for empty input
    }
    _ => panic!("Unexpected result"),
}

Fields

§expected: usize

Expected number of bytes needed

§actual: usize

Actual number of bytes available in the input

§

InvalidEncoding

Invalid encoding encountered

This error occurs when the data is structurally invalid beyond just varint decoding issues. For example, if a value is out of range for the target type or violates format-specific constraints.

This variant is provided for future extensibility and custom validation logic.

Fields

§reason: String

Human-readable description of why the encoding is invalid

Trait Implementations§

Source§

impl Debug for Error

Source§

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

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

impl Display for Error

Source§

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

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

impl Error for Error

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

Auto Trait Implementations§

§

impl Freeze for Error

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnsafeUnpin for Error

§

impl UnwindSafe for Error

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.