ohkami_lib/
map.rs

1/// Key-Value map mainly used to store custom headers.
2/// 
3/// Usually, a web app handles 0 ~ 4 custom headers, and so
4/// simple `Vec<(K, V)>` is efficient than `HashMap<K, V>`
5/// to store/iterate/mutate.
6pub struct TupleMap<K: PartialEq, V>(
7    Vec<(K, V)>
8);
9
10impl<K: PartialEq, V> TupleMap<K, V> {
11    pub fn new() -> Self {
12        Self(Vec::new())
13    }
14    pub fn from_iter<const N: usize>(iter: [(K, V); N]) -> Self {
15        Self(Vec::from(iter))
16    }
17
18    #[inline]
19    pub fn get(&self, key: &K) -> Option<&V> {
20        for (k, v) in &self.0 {
21            if key == k {return Some(v)}
22        }; None
23    }
24    #[inline]
25    pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
26        for (k, v) in &mut self.0 {
27            if key == k {return Some(v)}
28        }; None
29    }
30
31    #[inline]
32    pub fn insert(&mut self, key: K, value: V) -> Option<V> {
33        for (k, v) in &mut self.0 {
34            if &key == k {return Some(std::mem::replace(v, value))}
35        }; {self.0.push((key, value)); None}
36    }
37
38    #[inline]
39    pub fn remove(&mut self, key: K) -> Option<V> {
40        for i in 0..self.0.len() {
41            if &key == &unsafe {self.0.get_unchecked(i)}.0 {
42                return Some(self.0.swap_remove(i).1)
43            }
44        }; None
45    }
46
47    pub fn append(&mut self, another: Self) {
48        for (k, v) in another.into_iter() {
49            self.insert(k, v);
50        }
51    }
52
53    pub fn clear(&mut self) {
54        self.0.clear();
55    }
56
57    pub fn iter(&self) -> impl Iterator<Item = &(K, V)> {
58        self.0.iter()
59    }
60    pub fn into_iter(self) -> impl Iterator<Item = (K, V)> {
61        self.0.into_iter()
62    }
63}
64
65impl<K: PartialEq, V: PartialEq> PartialEq for TupleMap<K, V> {
66    fn eq(&self, other: &Self) -> bool {
67        self.0 == other.0
68    }
69}
70
71impl<K: Clone + PartialEq, V: Clone> Clone for TupleMap<K, V> {
72    fn clone(&self) -> Self {
73        Self(self.0.clone())
74    }
75}
76
77impl<K:PartialEq, V> std::fmt::Debug for TupleMap<K, V>
78where
79    K: std::fmt::Debug,
80    V: std::fmt::Debug,
81{
82    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
83        f.debug_map()
84            .entries(self.iter().map(|&(ref k, ref v)| (k, v)))
85            .finish()
86    }
87}
88
89impl<'de, K:PartialEq, V> serde::Deserialize<'de> for TupleMap<K, V>
90where
91    K: serde::Deserialize<'de>,
92    V: serde::Deserialize<'de>,
93{
94    fn deserialize<D: serde::Deserializer<'de>>(
95        deserializer: D
96    ) -> Result<Self, D::Error> {
97        return deserializer.deserialize_map(TupleMapVisitor(std::marker::PhantomData));
98
99        /////////////////////////////////////////////////////////////////////////
100        
101        struct TupleMapVisitor<K, V>(std::marker::PhantomData<fn()->(K, V)>);
102
103        impl<'de, K:PartialEq, V> serde::de::Visitor<'de> for TupleMapVisitor<K, V>
104        where
105            K: serde::Deserialize<'de>,
106            V: serde::Deserialize<'de>,
107        {
108            type Value = TupleMap<K, V>;
109
110            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
111                f.write_str("a map")
112            }
113
114            #[inline]
115            fn visit_map<A: serde::de::MapAccess<'de>>(self, mut access: A) -> Result<Self::Value, A::Error> {
116                let mut map = TupleMap::new();
117                while let Some((k, v)) = access.next_entry()? {
118                    map.insert(k, v);
119                }
120                Ok(map)
121            }
122        }
123    }
124}