#[non_exhaustive]pub enum SerialError {
UnexpectedEof {
needed: usize,
remaining: usize,
},
InvalidLength {
declared: u64,
remaining: usize,
},
VarintOverflow,
IntegerOutOfRange,
InvalidBool {
byte: u8,
},
InvalidUtf8,
InvalidTag {
kind: &'static str,
tag: u8,
},
TrailingBytes {
remaining: usize,
},
Io {
kind: ErrorKind,
message: String,
},
}Expand description
Every error returned by the codec.
#[non_exhaustive] so additional variants can be added in a MINOR release
without breaking downstream match arms. Callers MUST include a wildcard
arm.
§Examples
use pack_io::{decode, SerialError};
// A length prefix that runs off the end of the buffer is rejected, not
// accepted-and-corrected.
let bad: &[u8] = &[0xff, 0xff, 0xff, 0xff, 0x0f]; // varint = u32::MAX
match decode::<String>(bad) {
Ok(_) => unreachable!("hostile length should not decode"),
Err(SerialError::InvalidLength { .. })
| Err(SerialError::UnexpectedEof { .. }) => {} // expected
Err(other) => panic!("unexpected error variant: {other}"),
}Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
UnexpectedEof
The decoder needed more bytes than the input contained.
needed is the number of additional bytes the codec required to make
progress; remaining is what was actually left in the buffer.
Fields
InvalidLength
A length prefix declared a value larger than the buffer can hold.
This is the primary defence against a hostile length-prefix attack: the decoder refuses to allocate or read past the available input.
Fields
VarintOverflow
A LEB128 varint exceeded the maximum legal byte count for its target
width (10 bytes for u64, 5 for u32, 19 for u128, etc.).
IntegerOutOfRange
A decoded varint did not fit in the requested integer width
(e.g. u64 decoded successfully but the target was u32).
InvalidBool
A boolean byte was neither 0x00 nor 0x01.
Fields
InvalidUtf8
A length-prefixed byte run was not valid UTF-8 when decoding a
String.
InvalidTag
A tag byte for Option (0x00 / 0x01) or Result (0x00 / 0x01)
was outside the legal range.
Fields
TrailingBytes
The input buffer contained trailing bytes after a strict decode
completed. Returned only by crate::decode, which requires the
payload to be fully consumed.
Io
std only.An underlying std::io::Write / std::io::Read operation failed
while a streaming codec was in flight. Returned only by the
std-gated I/O integration (IoEncoder, IoDecoder,
encode_into, decode_from).
The error kind and a stringified message are captured so the variant
remains Clone + Eq. The original std::io::Error is not preserved
— log the captured message field for diagnostics.
Trait Implementations§
Source§impl Clone for SerialError
impl Clone for SerialError
Source§fn clone(&self) -> SerialError
fn clone(&self) -> SerialError
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SerialError
impl Debug for SerialError
Source§impl Display for SerialError
impl Display for SerialError
Source§impl Error for SerialError
Available on crate feature std only.
impl Error for SerialError
std only.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()
Source§impl PartialEq for SerialError
impl PartialEq for SerialError
Source§fn eq(&self, other: &SerialError) -> bool
fn eq(&self, other: &SerialError) -> bool
self and other values to be equal, and is used by ==.