#[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
thiserrorfor 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
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.
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
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.
Trait Implementations§
Source§impl Error for Error
impl Error for Error
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()