Type Alias zerocopy::error::TryReadError

source ·
pub type TryReadError<Src, Dst: ?Sized + TryFromBytes> = ConvertError<Infallible, SizeError<Src, Dst>, ValidityError<Src, Dst>>;
Expand description

The error type of fallible read-conversions.

Fallible read-conversions, like TryFromBytes::try_read_from_bytes may emit size and validity errors, but not alignment errors.

Aliased Type§

enum TryReadError<Src, Dst: ?Sized + TryFromBytes> {
    Alignment(Infallible),
    Size(SizeError<Src, Dst>),
    Validity(ValidityError<Src, Dst>),
}

Variants§

§

Alignment(Infallible)

The conversion source was improperly aligned.

§

Size(SizeError<Src, Dst>)

The conversion source was of incorrect size.

§

Validity(ValidityError<Src, Dst>)

The conversion source contained invalid data.

Implementations§

source§

impl<Src, Dst: ?Sized + TryFromBytes> TryReadError<Src, Dst>

source

pub fn into_src(self) -> Src

Produces the source underlying the failed conversion.

source

pub fn map_src<NewSrc>( self, f: impl Fn(Src) -> NewSrc, ) -> TryReadError<NewSrc, Dst>

Maps the source value associated with the conversion error.

This can help mitigate issues with Send, Sync and 'static bounds.

§Examples
use core::num::NonZeroU32;
use zerocopy::*;

let source: [u8; 3] = [0, 0, 0];

// Try to read a `NonZeroU32` from `source`.
let maybe_u32: Result<NonZeroU32, TryReadError<&[u8], NonZeroU32>>
    = NonZeroU32::try_read_from_bytes(&source[..]);

// Map the error's source to its size.
let maybe_u32: Result<NonZeroU32, TryReadError<usize, NonZeroU32>> =
    maybe_u32.map_err(|err| {
        err.map_src(|src| src.len())
    });