Type Alias zerocopy::error::TryCastError

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

The error type of fallible reference conversions.

Fallible reference conversions, like TryFromBytes::try_ref_from_bytes may emit alignment, size, and validity errors.

Aliased Type§

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

Variants§

§

Alignment(AlignmentError<Src, Dst>)

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> TryCastError<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, ) -> TryCastError<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, TryCastError<&[u8], NonZeroU32>>
    = NonZeroU32::try_ref_from_bytes(&source[..]);

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

Trait Implementations§

source§

impl<Src, Dst: ?Sized + TryFromBytes> From<ConvertError<AlignmentError<Src, Dst>, SizeError<Src, Dst>, Infallible>> for TryCastError<Src, Dst>

source§

fn from(value: CastError<Src, Dst>) -> Self

Converts to this type from the input type.