Skip to main content

rkyv/validation/shared/
mod.rs

1//! Shared pointer validation.
2
3#[cfg(feature = "alloc")]
4mod validator;
5
6use core::any::TypeId;
7
8use rancor::{Fallible, Strategy};
9
10#[cfg(feature = "alloc")]
11pub use self::validator::*;
12use crate::de::{ErasedPtr, Metadata};
13
14/// The result of starting to validate a shared pointer.
15pub enum ValidationState {
16    /// The caller started validating this value. They should proceed to check
17    /// the shared value and call `finish_shared`.
18    Started,
19    /// Another caller started validating this value, but has not finished yet.
20    /// This can only occur with cyclic shared pointer structures, and so rkyv
21    /// treats this as an error by default.
22    Pending,
23    /// This value has already been validated.
24    Finished,
25}
26
27/// A context that can validate shared archive memory.
28///
29/// Shared pointers require this kind of context to validate.
30pub trait SharedContext<E = <Self as Fallible>::Error> {
31    /// Starts validating the value associated with an erased data pointer and
32    /// shared pointer type.
33    ///
34    /// The arguments to this method are relatively complex:
35    ///
36    /// - `shared_type_id` is the type of the shared pointer to start validating
37    /// - `ptr` is an erased pointer to the data in the buffer that is shared
38    /// - `metadata_is_eq` is a comparison function called on potentially-equal
39    ///   data pointers
40    ///
41    /// `shared_type_id` and `ptr` are used as a unique key to identify the
42    /// shared pointer. If the shared context finds another shared pointer with
43    /// the same shared type ID and data address, it will call `metadata_is_eq`
44    /// on the metadata of the existing shared pointer and the metadata of the
45    /// provided shared pointer.
46    ///
47    /// Returns an error if the value associated with the given address was
48    /// started with a different type ID.
49    fn start_shared(
50        &mut self,
51        shared_type_id: TypeId,
52        ptr: ErasedPtr,
53        metadata_is_eq: unsafe fn(Metadata, Metadata) -> bool,
54    ) -> Result<ValidationState, E>;
55
56    /// Finishes validating the value associated with the given address.
57    ///
58    /// Returns an error if the given address was not pending.
59    fn finish_shared(
60        &mut self,
61        shared_type_id: TypeId,
62        ptr: ErasedPtr,
63    ) -> Result<(), E>;
64}
65
66impl<T, E> SharedContext<E> for Strategy<T, E>
67where
68    T: SharedContext<E>,
69{
70    fn start_shared(
71        &mut self,
72        shared_type_id: TypeId,
73        ptr: ErasedPtr,
74        metadata_is_eq: unsafe fn(Metadata, Metadata) -> bool,
75    ) -> Result<ValidationState, E> {
76        T::start_shared(self, shared_type_id, ptr, metadata_is_eq)
77    }
78
79    fn finish_shared(
80        &mut self,
81        shared_type_id: TypeId,
82        ptr: ErasedPtr,
83    ) -> Result<(), E> {
84        T::finish_shared(self, shared_type_id, ptr)
85    }
86}