Skip to main content

Error

Enum Error 

Source
#[non_exhaustive]
pub enum Error { UnexpectedEof, BufferFull, VarintOverflow, FrameTooLarge { len: usize, limit: usize, }, InvalidLengthPrefix, DelimiterNotFound, InvalidEncoding, BitOverflow, EmptyDelimiter, DelimiterInPayload, ChecksumMismatch, }
Expand description

Errors returned by codec and framing operations.

The variants fall into three groups:

§Example

use wire_codec::{Error, ReadBuf};

let mut buf = ReadBuf::new(&[0x01]);
// Only one byte available but we ask for two.
let result = buf.read_u16_be();
assert!(matches!(result, Err(Error::UnexpectedEof)));
assert_eq!(buf.position(), 0, "the cursor did not move");

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.
§

UnexpectedEof

Reader ran out of bytes before the requested value could be decoded.

When decoding a stream, wait for more input and retry the same call. When decoding a complete message, the message is truncated.

§

BufferFull

Writer ran out of capacity before the requested value could be encoded.

Flush or grow the output buffer and retry. The cursor did not move, and the crate’s own writers check room before writing, so they leave the buffer untouched (see the crate-level error contract for the composite exceptions).

§

VarintOverflow

A varint exceeded the maximum encoded length for its target width, or its value does not fit in the target type.

The input is corrupt or hostile.

§

FrameTooLarge

A frame’s length exceeds the configured limit for the framer.

On decode, the peer announced (or, for delimited framing, has already sent at least) len payload bytes against a limit of limit. Treat it as hostile input. On encode, the local payload is larger than the framer allows. Both fields saturate at usize::MAX.

Fields

§len: usize

Frame payload length advertised by the protocol, or a lower bound on it when the exact length is not yet known.

§limit: usize

Configured upper bound enforced by the framer.

§

InvalidLengthPrefix

A length prefix was structurally invalid: after applying the framer’s length adjustment it describes a negative payload length, or a payload cannot be represented under the configured adjustment.

§

DelimiterNotFound

A delimiter byte sequence was not found within the available input.

Not produced by the built-in framers, which report an over-long undelimited input as Error::FrameTooLarge. Available to custom Framer implementations.

§

InvalidEncoding

Decoded data violated a structural invariant required by the codec.

Produced by the canonical varint decoders and the varint-prefixed framer for overlong encodings, by cobs decoding for malformed blocks, by bool decoding for bytes other than 0 and 1, by packed structs whose padding bits are not zero, and by the bytes codec when a custom framer breaks the Frame contract. The input (or the framer) is at fault.

§

BitOverflow

A bit-level write requested more bits than the target type can hold, or a value did not fit in the requested bit width.

§

EmptyDelimiter

A delimited-framer constructor was given an empty delimiter, which cannot uniquely separate frames.

§

DelimiterInPayload

A payload passed to a delimited framer’s write_frame contains the delimiter sequence, so the receiver would split it into several frames.

Escape the payload (for example with cobs) or switch to a length-prefixed framer. Nothing was written.

§

ChecksumMismatch

A frame’s checksum trailer did not match the checksum computed over the received bytes.

The frame was corrupted in transit. Discard it. A CRC does not authenticate the sender; see checksum.

Trait Implementations§

Source§

impl Clone for Error

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 Error

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, f: &mut Formatter<'_>) -> Result

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

impl Eq for Error

Source§

impl Error for Error

Implemented unconditionally, including on no_std targets. std::error::Error is the same trait, so Error converts into Box<dyn std::error::Error>, anyhow::Error, and friends whether or not the std feature is enabled.

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 From<Error> for Error

Available on crate feature std only.

Convert into an std::io::Error, for use in I/O-based codecs.

The original Error is kept as the source and can be recovered with io::Error::get_ref and downcast_ref. The std::io::ErrorKind is chosen by group: Error::UnexpectedEof maps to UnexpectedEof, Error::BufferFull to WriteZero, caller misuse (Error::BitOverflow, Error::EmptyDelimiter, Error::DelimiterInPayload) to InvalidInput, and everything else to InvalidData.

§Example

use std::io;
use wire_codec::Error;

let io_err = io::Error::from(Error::ChecksumMismatch);
assert_eq!(io_err.kind(), io::ErrorKind::InvalidData);
let inner = io_err.get_ref().and_then(|e| e.downcast_ref::<Error>());
assert_eq!(inner, Some(&Error::ChecksumMismatch));
Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Error

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 Error

Auto Trait Implementations§

§

impl Forget for Error

§

impl Freeze for Error

§

impl Move 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> 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.