stable_map/
clone.rs

1#[cfg(test)]
2mod tests;
3
4use {
5    crate::StableMap,
6    core::hash::{BuildHasher, Hash},
7};
8
9impl<K, V, S> Clone for StableMap<K, V, S>
10where
11    K: Eq + Hash + Clone,
12    V: Clone,
13    S: BuildHasher + Clone,
14{
15    fn clone(&self) -> Self {
16        let mut map = Self::with_capacity_and_hasher(self.len(), self.hasher().clone());
17        for (k, v) in self {
18            unsafe {
19                // SAFETY:
20                // - All k are part of the same hash map so they must be distinct.
21                map.insert_unique_unchecked(k.clone(), v.clone());
22            }
23        }
24        map
25    }
26}