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
#[cfg(feature = "core")]
use core::hash::Hash;
use indexmap::IndexMap;
use rkyv::Archive;
#[cfg(feature = "std")]
use std::hash::Hash;

#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
/// See [`IndexMap`]
pub struct ArchivableIndexMap<K: Hash + Ord + Archive, V: Archive> {
    entries: Vec<(K, V)>,
}

impl<K: Hash + Ord + Archive, V: Archive> ArchivedArchivableIndexMap<K, V> {
    pub fn iter(&self) -> core::slice::Iter<'_, (K::Archived, V::Archived)> {
        self.entries.iter()
    }
}

impl<K: Hash + Ord + Archive + Clone, V: Archive> From<IndexMap<K, V>>
    for ArchivableIndexMap<K, V>
{
    fn from(it: IndexMap<K, V>) -> ArchivableIndexMap<K, V> {
        let mut r = ArchivableIndexMap {
            entries: Vec::new(),
        };
        for (k, v) in it.into_iter() {
            r.entries.push((k, v));
        }
        r
    }
}

impl<K: Hash + Ord + Archive + Clone, V: Archive> Into<IndexMap<K, V>>
    for ArchivableIndexMap<K, V>
{
    fn into(self) -> IndexMap<K, V> {
        let mut r = IndexMap::new();
        for (k, v) in self.entries.into_iter() {
            r.insert(k, v);
        }
        r
    }
}