[go: up one dir, main page]

rkyv/collections/
util.rs

1//! Utilities for archived collections.
2
3use core::{borrow::Borrow, error::Error, fmt, marker::PhantomData};
4
5use munge::munge;
6use rancor::Fallible;
7
8use crate::{Archive, Place, Portable, Serialize};
9
10/// An adapter which serializes and resolves its key and value references.
11pub struct EntryAdapter<BK, BV, K, V> {
12    /// The key to serialize and resolve.
13    pub key: BK,
14    /// The value to serialize and resolve.
15    pub value: BV,
16
17    _phantom: PhantomData<(K, V)>,
18}
19
20impl<BK, BV, K, V> EntryAdapter<BK, BV, K, V> {
21    /// Returns a new `EntryAdapter` for the given key and value.
22    pub fn new(key: BK, value: BV) -> Self {
23        Self {
24            key,
25            value,
26            _phantom: PhantomData,
27        }
28    }
29}
30
31/// A resolver for a key-value pair.
32pub struct EntryResolver<K, V> {
33    /// The key resolver.
34    pub key: K,
35    /// The value resolver.
36    pub value: V,
37}
38
39impl<BK, BV, K, V> Archive for EntryAdapter<BK, BV, K, V>
40where
41    BK: Borrow<K>,
42    BV: Borrow<V>,
43    K: Archive,
44    V: Archive,
45{
46    type Archived = Entry<K::Archived, V::Archived>;
47    type Resolver = EntryResolver<K::Resolver, V::Resolver>;
48
49    fn resolve(&self, resolver: Self::Resolver, out: Place<Self::Archived>) {
50        munge!(let Entry { key, value } = out);
51        K::resolve(self.key.borrow(), resolver.key, key);
52        V::resolve(self.value.borrow(), resolver.value, value);
53    }
54}
55
56impl<S, BK, BV, K, V> Serialize<S> for EntryAdapter<BK, BV, K, V>
57where
58    S: Fallible + ?Sized,
59    BK: Borrow<K>,
60    BV: Borrow<V>,
61    K: Serialize<S>,
62    V: Serialize<S>,
63{
64    fn serialize(
65        &self,
66        serializer: &mut S,
67    ) -> Result<Self::Resolver, S::Error> {
68        Ok(EntryResolver {
69            key: self.key.borrow().serialize(serializer)?,
70            value: self.value.borrow().serialize(serializer)?,
71        })
72    }
73}
74
75/// A key-value entry.
76#[derive(Debug, Portable, PartialEq, Eq, PartialOrd, Ord, Hash)]
77#[cfg_attr(feature = "bytecheck", derive(bytecheck::CheckBytes))]
78#[rkyv(crate)]
79#[repr(C)]
80pub struct Entry<K, V> {
81    /// The entry's key.
82    pub key: K,
83    /// The entry's value.
84    pub value: V,
85}
86
87/// An error describing that an iterator's length did not match the number of
88/// elements it yielded.
89#[derive(Debug)]
90pub struct IteratorLengthMismatch {
91    /// The number of expected elements.
92    pub expected: usize,
93    /// The actual number of elements.
94    pub actual: usize,
95}
96
97impl fmt::Display for IteratorLengthMismatch {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        write!(
100            f,
101            "iterator claimed that it contained {} elements, but yielded {} \
102             items during iteration",
103            self.expected, self.actual,
104        )
105    }
106}
107
108impl Error for IteratorLengthMismatch {}