duckdb/types/
ordered_map.rs

1/// An ordered map of key-value pairs.
2#[derive(Clone, Debug, Eq, PartialEq)]
3pub struct OrderedMap<K, V>(Vec<(K, V)>);
4
5impl<K: PartialEq, V> From<Vec<(K, V)>> for OrderedMap<K, V> {
6    fn from(value: Vec<(K, V)>) -> Self {
7        Self(value)
8    }
9}
10
11impl<K: std::cmp::PartialEq, V> OrderedMap<K, V> {
12    /// Returns the value corresponding to the key.
13    pub fn get(&self, key: &K) -> Option<&V> {
14        self.0.iter().find(|(k, _)| k == key).map(|(_, v)| v)
15    }
16    /// Returns an iterator over the keys in the map.
17    pub fn keys(&self) -> impl Iterator<Item = &K> {
18        self.0.iter().map(|(k, _)| k)
19    }
20    /// Returns an iterator over the values in the map.
21    pub fn values(&self) -> impl Iterator<Item = &V> {
22        self.0.iter().map(|(_, v)| v)
23    }
24    /// Returns an iterator over the key-value pairs in the map.
25    pub fn iter(&self) -> impl Iterator<Item = &(K, V)> {
26        self.0.iter()
27    }
28}