collect_with/collect_ahash.rs
1use core::hash::Hash;
2
3pub use ahash::{AHashMap, AHashSet};
4
5use crate::collect::CollectWith;
6
7// pub trait CollectHashContainer: Iterator {
8// type Container<K, V>: crate::ExtendWithCapacity<(K, V)>;
9
10// fn collect_map_with<K, V>(
11// self,
12// capacity: impl FnOnce(usize) -> usize,
13// ) -> Self::Container<K, V>
14// where
15// Self: Sized + Iterator<Item = (K, V)>,
16// K: Hash + Eq;
17// }
18
19impl<I: Iterator> CollectAHash for I {}
20
21/// Trait for collecting items into AHashMap or AHashSet with a specified
22/// capacity.
23pub trait CollectAHash: Iterator {
24 /// Collects items into an `AHashMap` with a specified capacity.
25 ///
26 /// # Example
27 ///
28 /// ```
29 /// use ahash::AHashMap;
30 /// use collect_with::CollectAHash;
31 ///
32 /// let map = ('a'..='i')
33 /// .zip(1..=9)
34 /// .collect_ahashmap_with(|u| u + 5);
35 /// assert_eq!(map.get(&'a'), Some(&1));
36 /// assert_eq!(map.len(), 9);
37 /// assert_eq!(map.capacity(), 14);
38 /// ```
39 fn collect_ahashmap_with<K, V>(
40 self,
41 capacity: impl FnOnce(usize) -> usize,
42 ) -> AHashMap<K, V>
43 where
44 Self: Sized + Iterator<Item = (K, V)>,
45 K: Hash + Eq,
46 {
47 self.collect_with(capacity)
48 }
49
50 /// Collects items into an `AHashMap` with an exact specified capacity.
51 ///
52 /// # Example
53 ///
54 /// ```
55 /// use ahash::AHashMap;
56 /// use collect_with::CollectAHash;
57 ///
58 /// let map = [(1, "a"), (2, "b"), (3, "c")]
59 /// .into_iter()
60 /// .collect_ahashmap_with_exact(|size_hint| size_hint);
61 /// assert_eq!(map.len(), 3);
62 /// ```
63 fn collect_ahashmap_with_exact<K, V>(
64 self,
65 capacity: impl FnOnce(usize) -> usize,
66 ) -> AHashMap<K, V>
67 where
68 Self: Sized + Iterator<Item = (K, V)>,
69 K: Hash + Eq,
70 {
71 self.collect_with_exact(capacity)
72 }
73
74 /// Collects items into an `AHashSet` with a specified capacity.
75 ///
76 /// # Example
77 ///
78 /// ```
79 /// use ahash::AHashSet;
80 /// use collect_with::CollectAHash;
81 ///
82 /// let set = (0..3)
83 /// .collect_ahashset_with(|size_hint| size_hint);
84 /// assert_eq!(set.len(), 3);
85 /// ```
86 fn collect_ahashset_with<K>(
87 self,
88 capacity: impl FnOnce(usize) -> usize,
89 ) -> AHashSet<K>
90 where
91 Self: Sized + Iterator<Item = K>,
92 K: Hash + Eq,
93 {
94 self.collect_with(capacity)
95 }
96
97 /// Collects items into an `AHashSet` with an exact specified capacity.
98 ///
99 /// # Example
100 ///
101 /// ```
102 /// use ahash::AHashSet;
103 /// use collect_with::CollectAHash;
104 ///
105 /// let set = (0..3)
106 /// .into_iter()
107 /// .collect_ahashset_with_exact(|size_hint| size_hint);
108 /// assert_eq!(set.len(), 3);
109 /// ```
110 fn collect_ahashset_with_exact<K>(
111 self,
112 capacity: impl FnOnce(usize) -> usize,
113 ) -> AHashSet<K>
114 where
115 Self: Sized + Iterator<Item = K>,
116 K: Hash + Eq,
117 {
118 self.collect_with_exact(capacity)
119 }
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125
126 #[ignore]
127 #[test]
128 fn dbg_collect_ahash_map() {
129 let map = ('a'..='i')
130 .zip(1..20)
131 .collect_ahashmap_with(|x| {
132 dbg!(x);
133 x
134 });
135 assert_eq!(map.get(&'a'), Some(&1));
136 dbg!(map);
137 }
138
139 #[ignore]
140 #[test]
141 fn dbg_collect_ahash_set() {
142 let map = ('a'..='i')
143 .zip(1..=9)
144 .collect_ahashset_with(|x| {
145 dbg!(x);
146 x
147 });
148 dbg!(map);
149 }
150}