Enum wire_rs::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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.