Type Alias AlignedTryCastError

type AlignedTryCastError = ConvertError<Infallible, SizeError<Src, Dst>, ValidityError<Src, Dst>>

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::*;
# use zerocopy_derive::*;

#[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)
    }
}