Enum WireError

Source
#[non_exhaustive]
pub enum WireError { InsufficientBytes, ExtraBytes, InvalidData(&'static str), Internal, }
Expand description

An error in reading from or writing to a wire.

New error types may be added to this enum, so it should not be exhaustively matched against.

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

InsufficientBytes

Returned when a Reader or Writer contains too few remaining bytes to fully read or write the desired type.

§

ExtraBytes

Returned when some bytes remain on the wire after the final data type is read or written.

The finalize() or finalize_after() methods can be used to check that all of the slice passed into a Reader or Writer is used. This is particularly useful for wire protocols that include a length field at the beginning of a packet that needs to be validated. When bytes remain in a Reader or Writer and one of the above methods is called, the InsufficientBytes error will be returned.

§

InvalidData(&'static str)

Returned when the type being read requires a particular format that the bytes on the wire do not adhere to, or when the type being written is not within a certain range of values that can be serialized.

The latter case can only occur for types that implement either the WireWritePart trait or the VectoredWritePart trait. For example, the following would return an InvalidData error:

use wire_rs::{WireError, WireWriter};

fn decode_partial_out_of_range() -> Result<(), WireError> {
    let mut buf = [0u8; 4];
    let out_of_range = 0x0100u16;
    let mut writer: WireWriter = WireWriter::new(buf.as_mut_slice());
    writer.write_part::<u16, 1>(&out_of_range) // Returns Err(WireError::InvalidData)
}

Whereas a number within the range of values that can be encoded for the given size would return Ok(()):

use wire_rs::{WireError, WireWriter};

fn decode_partial_within_range() -> Result<(), WireError> {
    let mut buf = [0u8; 4];
    let within_range = 0xFFu64;
    let mut writer: WireWriter = WireWriter::new(buf.as_mut_slice());
    writer.write_part::<u64, 1>(&within_range)
    // Returns Ok(())
}

As an example of the former case, a Reader would return an error when invalid UTF-8 is read in for a str type, such as:

use wire_rs::{WireError, WireReader};

fn decode_bad_utf8() -> Result<(), WireError> {
    let buf = [0xC3, 0x28]; // Invalid 2-octet UTF-8 sequence
    let mut reader: WireReader = WireReader::new(buf.as_slice());
    let s: &str = reader.read_remaining()?;
    // Returns Err(WireError::InvalidData)
    return Ok(())
}
§

Internal

An internal error in the Reader or Writer.

This will not be raised unless there is some bug in the implementation of the Reader of Writer, most likely caused by an invariant not holding. If encountered, this error should be counted as a fatal error in (de/)serializing data from the wire, and the Reader or Writer that returned this error should not be used for subsequent operations.

Trait Implementations§

Source§

impl Clone for WireError

Source§

fn clone(&self) -> WireError

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WireError

Source§

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

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

impl PartialEq for WireError

Source§

fn eq(&self, other: &WireError) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for WireError

Source§

impl Eq for WireError

Source§

impl StructuralPartialEq for WireError

Auto Trait Implementations§

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