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>
impl<Src, Dst: ?Sized + TryFromBytes> TryCastError<Src, Dst>
Sourcepub fn map_src<NewSrc>(
self,
f: impl FnOnce(Src) -> NewSrc,
) -> TryCastError<NewSrc, Dst>
pub fn map_src<NewSrc>( self, f: impl FnOnce(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>
impl<Src, Dst: ?Sized + TryFromBytes> From<ConvertError<AlignmentError<Src, Dst>, SizeError<Src, Dst>, Infallible>> for TryCastError<Src, Dst>
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)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§impl<Src, Dst: ?Sized, S, V> From<AlignmentError<Src, Dst>> for ConvertError<AlignmentError<Src, Dst>, S, V>
impl<Src, Dst: ?Sized, S, V> From<AlignmentError<Src, Dst>> for ConvertError<AlignmentError<Src, Dst>, S, V>
Source§fn from(err: AlignmentError<Src, Dst>) -> Self
fn from(err: AlignmentError<Src, Dst>) -> Self
Converts to this type from the input type.
Source§impl<Src, Dst: ?Sized, A, V> From<SizeError<Src, Dst>> for ConvertError<A, SizeError<Src, Dst>, V>
impl<Src, Dst: ?Sized, A, V> From<SizeError<Src, Dst>> for ConvertError<A, SizeError<Src, Dst>, V>
Source§impl<Src, Dst: ?Sized + TryFromBytes, A, S> From<ValidityError<Src, Dst>> for ConvertError<A, S, ValidityError<Src, Dst>>
impl<Src, Dst: ?Sized + TryFromBytes, A, S> From<ValidityError<Src, Dst>> for ConvertError<A, S, ValidityError<Src, Dst>>
Source§fn from(err: ValidityError<Src, Dst>) -> Self
fn from(err: ValidityError<Src, Dst>) -> Self
Converts to this type from the input type.