redact_composer_core/util.rs
1use std::hash::BuildHasherDefault;
2use twox_hash::XxHash64;
3
4/// Alias of [`std::collections::HashMap`] with `RandomState` replaced
5/// by a deterministic default hasher [`XxHash64`]. Determinism is important during composition
6/// rendering for reproducibility with seeds.
7///
8/// Unfortunately, with a different hasher, some of the convenience constructors such as
9/// [`HashMap::new`](std::collections::HashMap::new) and
10/// [`HashMap::from`](std::collections::HashMap::from) are lost. Here are some suggestions however:
11/// ```
12/// # use redact_composer_core::util::HashMap;
13/// // To get a new empty HashMap
14/// let map: HashMap<String, String> = HashMap::default();
15/// assert!(map.is_empty());
16///
17/// // To initialize with an array
18/// let map = [("key1", "val1"), ("key2", "val2")].into_iter().collect::<HashMap<_, _>>();
19/// assert_eq!(map.get("key1"), Some(&"val1"));
20/// assert_eq!(map.get("key2"), Some(&"val2"));
21/// ```
22pub type HashMap<K, V> = std::collections::HashMap<K, V, BuildHasherDefault<XxHash64>>;
23
24/// Alias of [`std::collections::HashSet`] with `RandomState` replaced by a deterministic default
25/// hasher [`XxHash64`]. Determinism is important during composition rendering for reproducibility
26/// with seeds.
27///
28/// Unfortunately, with a different hasher, some of the convenience constructors such as
29/// [`HashSet::new`](std::collections::HashSet::new) and
30/// [`HashSet::from`](std::collections::HashSet::from) are lost. Here are some suggestions however:
31/// ```
32/// // To get a new empty HashSet
33/// # use redact_composer_core::util::HashSet;
34/// let set: HashSet<String> = HashSet::default();
35/// assert!(set.is_empty());
36///
37/// // To initialize with an array
38/// let set = ["val1", "val2"].into_iter().collect::<HashSet<_>>();
39/// assert!(set.contains("val1"));
40/// assert!(set.contains("val2"));
41/// ```
42pub type HashSet<T> = std::collections::HashSet<T, BuildHasherDefault<XxHash64>>;