gluesql_utils/
indexmap.rs

1use {
2    indexmap::map::{IntoIter, Keys},
3    std::{cmp::Eq, hash::Hash},
4};
5
6/// HashMap which provides
7/// 1. Immutable APIs
8/// 2. Preserving insertion order
9pub struct IndexMap<K, V>(indexmap::IndexMap<K, V>);
10
11impl<K: Hash + Eq, V> IndexMap<K, V> {
12    pub fn new() -> Self {
13        Self(indexmap::IndexMap::new())
14    }
15
16    pub fn insert(mut self, key: K, value: V) -> (Self, Option<V>) {
17        let existing = self.0.insert(key, value);
18
19        (self, existing)
20    }
21
22    pub fn get(&self, key: &K) -> Option<&V> {
23        self.0.get(key)
24    }
25
26    pub fn keys(&self) -> Keys<'_, K, V> {
27        self.0.keys()
28    }
29
30    pub fn len(&self) -> usize {
31        self.0.len()
32    }
33
34    pub fn is_empty(&self) -> bool {
35        self.0.is_empty()
36    }
37}
38
39impl<K: Hash + Eq, V> Default for IndexMap<K, V> {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45impl<K: Hash + Eq, V> IntoIterator for IndexMap<K, V> {
46    type Item = (K, V);
47    type IntoIter = IntoIter<K, V>;
48
49    fn into_iter(self) -> Self::IntoIter {
50        self.0.into_iter()
51    }
52}