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.
Implementations
Source§impl<Src, Dst: ?Sized + TryFromBytes> ConvertError<Infallible, SizeError<Src, Dst>, ValidityError<Src, Dst>>
impl<Src, Dst: ?Sized + TryFromBytes> ConvertError<Infallible, SizeError<Src, Dst>, ValidityError<Src, Dst>>
Sourcepub fn map_src<NewSrc>(
self,
f: impl FnOnce(Src) -> NewSrc,
) -> TryReadError<NewSrc, Dst>
pub fn map_src<NewSrc>( self, f: impl FnOnce(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())
});
Trait Implementations
Source§impl<A: Display, S: Display, V: Display> Display for ConvertError<A, S, V>
Produces a human-readable error message.
impl<A: Display, S: Display, V: Display> Display for ConvertError<A, S, V>
Produces a human-readable error message.
The message differs between debug and release builds. When
debug_assertions
are enabled, this message is verbose and includes
potentially sensitive information.
Source§impl<A, S, V> Error for ConvertError<A, S, V>
impl<A, S, V> Error for ConvertError<A, S, V>
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl<Src, Dst: ?Sized + Unaligned, S, V> From<ConvertError<AlignmentError<Src, Dst>, S, V>> for ConvertError<Infallible, S, V>
impl<Src, Dst: ?Sized + Unaligned, S, V> From<ConvertError<AlignmentError<Src, Dst>, S, V>> for ConvertError<Infallible, S, V>
Source§fn from(
err: ConvertError<AlignmentError<Src, Dst>, S, V>,
) -> ConvertError<Infallible, S, V>
fn from( err: ConvertError<AlignmentError<Src, Dst>, S, V>, ) -> ConvertError<Infallible, S, V>
Infallibly discards the alignment error from this ConvertError
since
Dst
is unaligned.
Since Dst: Unaligned
, it is impossible to encounter an alignment
error. This method permits discarding that alignment error infallibly
and replacing it with Infallible
.
§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)
}
}