rkyv/validation/
owned.rs

1//! Common validation utilities for owned containers (`Box`, `String`, `Vec`, etc.).
2
3use crate::{ArchivePointee, Fallible};
4use bytecheck::CheckBytes;
5use core::fmt;
6#[cfg(feature = "std")]
7use std::error::Error;
8
9/// Errors that can occur while chechking archived owned pointers
10#[derive(Debug)]
11pub enum OwnedPointerError<T, R, C> {
12    /// The pointer failed to validate due to invalid metadata.
13    PointerCheckBytesError(T),
14    /// The value pointed to by the owned pointer was invalid.
15    ValueCheckBytesError(R),
16    /// An error occurred from the validation context.
17    ContextError(C),
18}
19
20impl<T, R, C> fmt::Display for OwnedPointerError<T, R, C>
21where
22    T: fmt::Display,
23    R: fmt::Display,
24    C: fmt::Display,
25{
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            OwnedPointerError::PointerCheckBytesError(e) => e.fmt(f),
29            OwnedPointerError::ValueCheckBytesError(e) => e.fmt(f),
30            OwnedPointerError::ContextError(e) => e.fmt(f),
31        }
32    }
33}
34
35#[cfg(feature = "std")]
36impl<T, R, C> Error for OwnedPointerError<T, R, C>
37where
38    T: Error + 'static,
39    R: Error + 'static,
40    C: Error + 'static,
41{
42    fn source(&self) -> Option<&(dyn Error + 'static)> {
43        match self {
44            OwnedPointerError::PointerCheckBytesError(e) => Some(e as &dyn Error),
45            OwnedPointerError::ValueCheckBytesError(e) => Some(e as &dyn Error),
46            OwnedPointerError::ContextError(e) => Some(e as &dyn Error),
47        }
48    }
49}
50
51/// The [`OwnedPointerError`] for an owned `T` being checked with a some context `C`.
52pub type CheckOwnedPointerError<T, C> = OwnedPointerError<
53    <<T as ArchivePointee>::ArchivedMetadata as CheckBytes<C>>::Error,
54    <T as CheckBytes<C>>::Error,
55    <C as Fallible>::Error,
56>;