Debian Codegraph @

src/impls/into_iterator.rs

raw symbols SCIP
1 use crate::Counter;
2
3 use std::hash::Hash;
4
5 impl<'a, T, N> IntoIterator for &'a Counter<T, N>
6 where
7 T: Hash + Eq,
8 {
9 type Item = (&'a T, &'a N);
10 type IntoIter = std::collections::hash_map::Iter<'a, T, N>;
11
12 fn into_iter(self) -> Self::IntoIter {
13 self.map.iter()
14 }
15 }
16
17 impl<T, N> IntoIterator for Counter<T, N>
18 where
19 T: Hash + Eq,
20 {
21 type Item = (T, N);
22 type IntoIter = std::collections::hash_map::IntoIter<T, N>;
23
24 /// Consumes the `Counter` to produce an iterator that owns the values it returns.
25 ///
26 /// # Examples
27 /// ```rust
28 /// # use counter::Counter;
29 ///
30 /// let counter: Counter<_> = "aaab".chars().collect();
31 ///
32 /// let vec: Vec<_> = counter.into_iter().collect();
33 ///
34 /// for (item, count) in &vec {
35 /// if item == &'a' {
36 /// assert_eq!(count, &3);
37 /// }
38 /// if item == &'b' {
39 /// assert_eq!(count, &1);
40 /// }
41 /// }
42 /// ```
43
44 fn into_iter(self) -> Self::IntoIter {
45 self.map.into_iter()
46 }
47 }
48
49 impl<'a, T, N> IntoIterator for &'a mut Counter<T, N>
50 where
51 T: Hash + Eq,
52 {
53 type Item = (&'a T, &'a mut N);
54 type IntoIter = std::collections::hash_map::IterMut<'a, T, N>;
55
56 /// Creates an iterator that provides mutable references to the counts, but keeps the keys immutable.
57 ///
58 /// # Examples
59 /// ```rust
60 /// # use counter::Counter;
61 ///
62 /// let mut counter: Counter<_> = "aaab".chars().collect();
63 ///
64 /// for (item, count) in &mut counter {
65 /// if *item == 'a' {
66 /// // 'a' is so great it counts as 2
67 /// *count *= 2;
68 /// }
69 /// }
70 ///
71 /// assert_eq!(counter[&'a'], 6);
72 /// assert_eq!(counter[&'b'], 1);
73 /// ```
74 fn into_iter(self) -> Self::IntoIter {
75 self.map.iter_mut()
76 }
77 }