Type Alias zerocopy::error::CastError

source ·
pub type CastError<Src, Dst: ?Sized> = ConvertError<AlignmentError<Src, Dst>, SizeError<Src, Dst>, Infallible>;
Expand description

The error type of reference conversions.

Reference conversions, like FromBytes::ref_from_bytes may emit alignment and size errors.

Aliased Type§

enum CastError<Src, Dst: ?Sized> {
    Alignment(AlignmentError<Src, Dst>),
    Size(SizeError<Src, Dst>),
    Validity(Infallible),
}

Variants§

§

Alignment(AlignmentError<Src, Dst>)

The conversion source was improperly aligned.

§

Size(SizeError<Src, Dst>)

The conversion source was of incorrect size.

§

Validity(Infallible)

The conversion source contained invalid data.

Implementations§

source§

impl<Src, Dst: ?Sized> CastError<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, ) -> CastError<NewSrc, Dst>

Maps the source value associated with the conversion error.

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

§Examples
use zerocopy::*;

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

// Try to read a `u32` from `source`. This will fail because there are insufficient
// bytes in `source`.
let maybe_u32: Result<&u32, CastError<&[u8], u32>> = u32::ref_from_bytes(&source[..]);

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