hpl_toolkit/utils/
vec_map.rs

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use anchor_lang::prelude::*;
use std::collections::HashMap;

#[cfg(feature = "compression")]
use crate::compression::*;

#[cfg(feature = "schema")]
use crate::schema::*;

#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(AnchorSerialize, AnchorDeserialize, Clone, PartialEq)]
pub struct KeyValue<
    K: AnchorSerialize + AnchorDeserialize + Clone,
    V: AnchorSerialize + AnchorDeserialize + Clone,
> {
    key: K,
    value: V,
}
impl<
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > Into<(K, V)> for KeyValue<K, V>
{
    fn into(self) -> (K, V) {
        (self.key, self.value)
    }
}
impl<
        'i,
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > Into<(&'i K, &'i V)> for &'i KeyValue<K, V>
{
    fn into(self) -> (&'i K, &'i V) {
        (&self.key, &self.value)
    }
}
impl<
        'i,
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > Into<(&'i K, &'i mut V)> for &'i mut KeyValue<K, V>
{
    fn into(self) -> (&'i K, &'i mut V) {
        (&self.key, &mut self.value)
    }
}
impl<
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > From<(&K, &V)> for KeyValue<K, V>
{
    fn from((key, value): (&K, &V)) -> Self {
        KeyValue {
            key: key.to_owned(),
            value: value.to_owned(),
        }
    }
}
impl<
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > From<(K, V)> for KeyValue<K, V>
{
    fn from((key, value): (K, V)) -> Self {
        Self { key, value }
    }
}
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(AnchorSerialize, AnchorDeserialize, Clone, PartialEq)]
pub struct VecMap<
    K: AnchorSerialize + AnchorDeserialize + Clone,
    V: AnchorSerialize + AnchorDeserialize + Clone,
> {
    _inner: Vec<KeyValue<K, V>>,
}

impl<
        K: AnchorSerialize + AnchorDeserialize + Clone + PartialEq,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > VecMap<K, V>
{
    pub fn new() -> Self {
        Self { _inner: vec![] }
    }

    pub fn into_inner(self) -> Vec<KeyValue<K, V>> {
        self._inner
    }

    pub fn insert(&mut self, key: K, value: V) {
        for KeyValue { key: k, value: v } in &mut self._inner {
            if k == &key {
                *v = value;
                return;
            }
        }
        self._inner.push(KeyValue { key, value });
    }

    pub fn get(&self, key: &K) -> Option<&V> {
        self._inner
            .iter()
            .find_map(|KeyValue { key: k, value: v }| if k == key { Some(v) } else { None })
    }

    pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
        self._inner
            .iter_mut()
            .find_map(|KeyValue { key: k, value: v }| if k == key { Some(v) } else { None })
    }

    pub fn iter<'i>(&'i self) -> impl Iterator<Item = (&'i K, &'i V)> {
        let a = self._inner.iter().map(|i| -> (&K, &V) { i.into() });
        a
    }

    pub fn iter_mut<'i>(&'i mut self) -> impl Iterator<Item = (&'i K, &'i mut V)> {
        let a = self._inner.iter_mut().map(|i| -> (&K, &mut V) { i.into() });

        a
    }

    pub fn remove(&mut self, key: &K) {
        self._inner.retain(|KeyValue { key: k, .. }| k != key);
    }
}
// Custom iterator for VecMap
pub struct VecMapIter<
    K: AnchorSerialize + AnchorDeserialize + Clone,
    V: AnchorSerialize + AnchorDeserialize + Clone,
> {
    inner: std::vec::IntoIter<KeyValue<K, V>>,
}

impl<
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > Iterator for VecMapIter<K, V>
where
    K: Clone,
    V: Clone,
{
    type Item = (K, V);

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next().map(|kv| (kv.key, kv.value))
    }
}
impl<
        K: AnchorSerialize + AnchorDeserialize + Clone + PartialEq,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > FromIterator<(K, V)> for VecMap<K, V>
{
    fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
        let mut vec_map = VecMap::new();
        for (key, value) in iter {
            vec_map._inner.push(KeyValue { key, value });
        }
        vec_map
    }
}
impl<
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > IntoIterator for VecMap<K, V>
where
    K: AnchorSerialize + AnchorDeserialize + Clone,
    V: AnchorSerialize + AnchorDeserialize + Clone,
{
    type Item = (K, V);
    type IntoIter = VecMapIter<K, V>;

    fn into_iter(self) -> Self::IntoIter {
        VecMapIter {
            inner: self._inner.into_iter(),
        }
    }
}

impl<
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > FromIterator<KeyValue<K, V>> for VecMap<K, V>
{
    fn from_iter<T: IntoIterator<Item = KeyValue<K, V>>>(iter: T) -> Self {
        Self {
            _inner: iter.into_iter().collect(),
        }
    }
}

impl<
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > From<&HashMap<K, V>> for VecMap<K, V>
{
    fn from(map: &HashMap<K, V>) -> Self {
        Self {
            _inner: map
                .iter()
                .map(|(k, v)| KeyValue {
                    key: k.clone(),
                    value: v.clone(),
                })
                .collect(),
        }
    }
}

impl<
        K: AnchorSerialize + AnchorDeserialize + Clone,
        V: AnchorSerialize + AnchorDeserialize + Clone,
    > From<HashMap<K, V>> for VecMap<K, V>
{
    fn from(map: HashMap<K, V>) -> Self {
        Self {
            _inner: map
                .into_iter()
                .map(|(k, v)| KeyValue { key: k, value: v })
                .collect(),
        }
    }
}

#[cfg(feature = "compression")]
impl<
        K: AnchorSerialize + AnchorDeserialize + Clone + ToNode,
        V: AnchorSerialize + AnchorDeserialize + Clone + ToNode,
    > ToNode for VecMap<K, V>
{
    fn to_node(&self) -> [u8; 32] {
        let mut seeds: Vec<[u8; 32]> = vec![];
        for KeyValue { key, value } in &self._inner {
            seeds.push(key.to_node());
            seeds.push(value.to_node());
        }
        let seeds_refs: Vec<&[u8]> = seeds.iter().map(|node| &node[..]).collect();
        anchor_lang::solana_program::keccak::hashv(&seeds_refs[..]).to_bytes()
    }
}

#[cfg(feature = "schema")]
impl<
        K: AnchorSerialize + AnchorDeserialize + Clone + ToSchema + PartialEq,
        V: AnchorSerialize + AnchorDeserialize + Clone + ToSchema,
    > ToSchema for VecMap<K, V>
{
    fn schema() -> Schema {
        Schema::VecMap(Box::new(K::schema()), Box::new(V::schema()))
    }

    fn schema_value(&self) -> SchemaValue {
        let mut schema = VecMap::<SchemaValue, SchemaValue>::new();
        self._inner.iter().for_each(|KeyValue { key, value }| {
            schema.insert(key.schema_value(), value.schema_value());
        });
        SchemaValue::VecMap(schema)
    }
}