Type Alias zerocopy::error::AlignedTryCastError

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

The error type of well-aligned, fallible casts.

This is like TryCastError, but for casts that are always well-aligned. It is identical to TryCastError, except that its alignment error is Infallible.

As of this writing, none of zerocopy’s API produces this error directly. However, it is useful since it permits users to infallibly discard alignment errors when they can prove statically that alignment errors are impossible.

§Examples

use core::convert::Infallible;
use zerocopy::*;

#[derive(TryFromBytes, KnownLayout, Unaligned, Immutable)]
#[repr(C, packed)]
struct Bools {
    one: bool,
    two: bool,
    many: [bool],
}

impl Bools {
    fn parse(bytes: &[u8]) -> Result<&Bools, AlignedTryCastError<&[u8], Bools>> {
        // Since `Bools: Unaligned`, we can infallibly discard
        // the alignment error.
        Bools::try_ref_from_bytes(bytes).map_err(Into::into)
    }
}

Aliased Type§

enum AlignedTryCastError<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.