Type Alias 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 FnOnce(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))
});

Trait Implementations

Source§

impl<A: Debug, S: Debug, V: Debug> Debug for ConvertError<A, S, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A, S, V> Error for ConvertError<A, S, V>
where A: Display + Debug, S: Display + Debug, V: Display + Debug,

1.30.0 · Source§

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

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

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

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>

Source§

fn from(err: SizeError<Src, Dst>) -> Self

Converts to this type from the input type.
Source§

impl<A: PartialEq, S: PartialEq, V: PartialEq> PartialEq for ConvertError<A, S, V>

Source§

fn eq(&self, other: &ConvertError<A, S, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: Eq, S: Eq, V: Eq> Eq for ConvertError<A, S, V>

Source§

impl<A, S, V> StructuralPartialEq for ConvertError<A, S, V>