junobuild_shared/structures.rs
1use ic_stable_structures::btreemap::{BTreeMap, Iter};
2use ic_stable_structures::{Memory, Storable};
3use std::collections::HashMap;
4
5/// Collects entries from a stable `BTreeMap` iterator into a `Vec<(K, V)>`.
6///
7/// This is useful when working with `ic-stable-structures`'s `BTreeMap`
8/// and you want to extract all entries as a vector of key-value pairs.
9///
10/// # Type Parameters
11/// - `K`: The key type, which must be `Storable`, `Clone`, and `Ord`.
12/// - `V`: The value type, which must be `Storable` and `Clone`.
13/// - `M`: The memory backend, which must implement the `Memory` trait.
14///
15/// # Arguments
16/// - `iter`: An iterator over the entries of a stable `BTreeMap`.
17///
18/// # Returns
19/// A vector containing `(K, V)` pairs.
20///
21/// # Example (using `iter`)
22/// ```
23/// let vec = collect_stable_vec(my_stable_map.iter());
24/// ```
25///
26/// # Example (using `range`)
27/// ```
28/// let range = my_stable_map.range(2..3);
29/// let entries = collect_stable_vec(range);
30/// ```
31pub fn collect_stable_vec<K, V, M>(iter: Iter<'_, K, V, M>) -> Vec<(K, V)>
32where
33 K: Storable + Clone + Ord,
34 V: Storable + Clone,
35 M: Memory,
36{
37 iter.map(|entry| (entry.key().clone(), entry.value().clone()))
38 .collect()
39}
40
41/// Collects entries from a stable `BTreeMap` iterator into a `HashMap<K, V>`.
42///
43/// This helper is useful when converting from a stable structure
44/// (like `BTreeMap<K, V, M>`) to an in-memory `HashMap`.
45///
46/// # Type Parameters
47/// - `K`: The key type, which must be `Storable`, `Clone`, `Ord`, `Eq`, and `Hash`.
48/// - `V`: The value type, which must be `Storable` and `Clone`.
49/// - `M`: The memory backend, which must implement the `Memory` trait.
50///
51/// # Arguments
52/// - `iter`: An iterator over the entries of a stable `BTreeMap`.
53///
54/// # Returns
55/// A `HashMap<K, V>` containing the collected entries.
56///
57/// # Example (using `iter`)
58/// ```
59/// let map = collect_stable_map(my_stable_map.iter());
60/// ```
61///
62/// # Example (using `range`)
63/// ```
64/// let range = my_stable_map.range(2..3);
65/// let entries = collect_stable_map(range);
66/// ```
67pub fn collect_stable_map<K, V, M>(iter: Iter<'_, K, V, M>) -> HashMap<K, V>
68where
69 K: Storable + Clone + Eq + Ord + std::hash::Hash,
70 V: Storable + Clone,
71 M: Memory,
72{
73 iter.map(|entry| (entry.key().clone(), entry.value().clone()))
74 .collect()
75}
76
77/// Collects entries from a `BTreeMap` into a `Vec<(K, V)>`.
78///
79/// This is a convenience wrapper over [`collect_stable_vec`] that calls `.iter()` for you.
80///
81/// # Type Parameters
82/// - `K`: Key type (must be `Storable`, `Clone`, and `Ord`)
83/// - `V`: Value type (must be `Storable` and `Clone`)
84/// - `M`: Stable memory backend
85///
86/// # Example
87/// ```ignore
88/// let vec = collect_stable_vec_from(&my_stable_map);
89/// ```
90pub fn collect_stable_vec_from<K, V, M>(map: &BTreeMap<K, V, M>) -> Vec<(K, V)>
91where
92 K: Storable + Clone + Ord,
93 V: Storable + Clone,
94 M: Memory,
95{
96 collect_stable_vec(map.iter())
97}
98
99/// Collects entries from a `BTreeMap` into a `HashMap<K, V>`.
100///
101/// This is a convenience wrapper over [`collect_stable_map`] that calls `.iter()` for you.
102///
103/// # Type Parameters
104/// - `K`: Key type (must be `Storable`, `Clone`, `Ord`, `Eq`, `Hash`)
105/// - `V`: Value type (must be `Storable` and `Clone`)
106/// - `M`: Stable memory backend
107///
108/// # Example
109/// ```ignore
110/// let map = collect_stable_map_from(&my_stable_map);
111/// ```
112pub fn collect_stable_map_from<K, V, M>(map: &BTreeMap<K, V, M>) -> HashMap<K, V>
113where
114 K: Storable + Clone + Eq + Ord + std::hash::Hash,
115 V: Storable + Clone,
116 M: Memory,
117{
118 collect_stable_map(map.iter())
119}