1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Utilities for archived collections.

#[cfg(feature = "validation")]
pub mod validation;

use crate::{Archive, Fallible, Serialize};

/// A simple key-value pair.
///
/// This is typically used by associative containers that store keys and values together.
#[derive(Debug, Eq)]
#[cfg_attr(feature = "strict", repr(C))]
pub struct Entry<K, V> {
    /// The key of the pair.
    pub key: K,
    /// The value of the pair.
    pub value: V,
}

impl<K: Archive, V: Archive> Archive for Entry<&'_ K, &'_ V> {
    type Archived = Entry<K::Archived, V::Archived>;
    type Resolver = (K::Resolver, V::Resolver);

    #[inline]
    unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) {
        let (fp, fo) = out_field!(out.key);
        self.key.resolve(pos + fp, resolver.0, fo);

        let (fp, fo) = out_field!(out.value);
        self.value.resolve(pos + fp, resolver.1, fo);
    }
}

impl<K: Serialize<S>, V: Serialize<S>, S: Fallible + ?Sized> Serialize<S> for Entry<&'_ K, &'_ V> {
    #[inline]
    fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
        Ok((
            self.key.serialize(serializer)?,
            self.value.serialize(serializer)?,
        ))
    }
}

impl<K, V, UK, UV> PartialEq<Entry<UK, UV>> for Entry<K, V>
where
    K: PartialEq<UK>,
    V: PartialEq<UV>,
{
    #[inline]
    fn eq(&self, other: &Entry<UK, UV>) -> bool {
        self.key.eq(&other.key) && self.value.eq(&other.value)
    }
}