rkyv/collections/util/
mod.rs

1//! Utilities for archived collections.
2
3#[cfg(feature = "validation")]
4pub mod validation;
5
6use crate::{Archive, Fallible, Serialize};
7
8/// A simple key-value pair.
9///
10/// This is typically used by associative containers that store keys and values together.
11#[derive(Debug, Eq)]
12#[cfg_attr(feature = "strict", repr(C))]
13pub struct Entry<K, V> {
14    /// The key of the pair.
15    pub key: K,
16    /// The value of the pair.
17    pub value: V,
18}
19
20impl<K: Archive, V: Archive> Archive for Entry<&'_ K, &'_ V> {
21    type Archived = Entry<K::Archived, V::Archived>;
22    type Resolver = (K::Resolver, V::Resolver);
23
24    #[inline]
25    unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) {
26        let (fp, fo) = out_field!(out.key);
27        self.key.resolve(pos + fp, resolver.0, fo);
28
29        let (fp, fo) = out_field!(out.value);
30        self.value.resolve(pos + fp, resolver.1, fo);
31    }
32}
33
34impl<K: Serialize<S>, V: Serialize<S>, S: Fallible + ?Sized> Serialize<S> for Entry<&'_ K, &'_ V> {
35    #[inline]
36    fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
37        Ok((
38            self.key.serialize(serializer)?,
39            self.value.serialize(serializer)?,
40        ))
41    }
42}
43
44impl<K, V, UK, UV> PartialEq<Entry<UK, UV>> for Entry<K, V>
45where
46    K: PartialEq<UK>,
47    V: PartialEq<UV>,
48{
49    #[inline]
50    fn eq(&self, other: &Entry<UK, UV>) -> bool {
51        self.key.eq(&other.key) && self.value.eq(&other.value)
52    }
53}