rayon/collections/
hash_map.rs

1//! This module contains the parallel iterator types for hash maps
2//! (`HashMap<K, V>`). You will rarely need to interact with it directly
3//! unless you have need to name one of the iterator types.
4
5use std::collections::HashMap;
6use std::hash::{BuildHasher, Hash};
7use std::marker::PhantomData;
8
9use crate::iter::plumbing::*;
10use crate::iter::*;
11
12use crate::vec;
13
14/// Parallel iterator over a hash map
15#[derive(Debug)] // std doesn't Clone
16pub struct IntoIter<K: Hash + Eq + Send, V: Send> {
17    inner: vec::IntoIter<(K, V)>,
18}
19
20into_par_vec! {
21    HashMap<K, V, S> => IntoIter<K, V>,
22    impl<K: Hash + Eq + Send, V: Send, S: BuildHasher>
23}
24
25delegate_iterator! {
26    IntoIter<K, V> => (K, V),
27    impl<K: Hash + Eq + Send, V: Send>
28}
29
30/// Parallel iterator over an immutable reference to a hash map
31#[derive(Debug)]
32pub struct Iter<'a, K: Hash + Eq + Sync, V: Sync> {
33    inner: vec::IntoIter<(&'a K, &'a V)>,
34}
35
36impl<'a, K: Hash + Eq + Sync, V: Sync> Clone for Iter<'a, K, V> {
37    fn clone(&self) -> Self {
38        Iter {
39            inner: self.inner.clone(),
40        }
41    }
42}
43
44into_par_vec! {
45    &'a HashMap<K, V, S> => Iter<'a, K, V>,
46    impl<'a, K: Hash + Eq + Sync, V: Sync, S: BuildHasher>
47}
48
49delegate_iterator! {
50    Iter<'a, K, V> => (&'a K, &'a V),
51    impl<'a, K: Hash + Eq + Sync + 'a, V: Sync + 'a>
52}
53
54/// Parallel iterator over a mutable reference to a hash map
55#[derive(Debug)]
56pub struct IterMut<'a, K: Hash + Eq + Sync, V: Send> {
57    inner: vec::IntoIter<(&'a K, &'a mut V)>,
58}
59
60into_par_vec! {
61    &'a mut HashMap<K, V, S> => IterMut<'a, K, V>,
62    impl<'a, K: Hash + Eq + Sync, V: Send, S: BuildHasher>
63}
64
65delegate_iterator! {
66    IterMut<'a, K, V> => (&'a K, &'a mut V),
67    impl<'a, K: Hash + Eq + Sync + 'a, V: Send + 'a>
68}
69
70/// Draining parallel iterator that moves out of a hash map,
71/// but keeps the total capacity.
72#[derive(Debug)]
73pub struct Drain<'a, K: Hash + Eq + Send, V: Send> {
74    inner: vec::IntoIter<(K, V)>,
75    marker: PhantomData<&'a mut HashMap<K, V>>,
76}
77
78impl<'a, K: Hash + Eq + Send, V: Send, S: BuildHasher> ParallelDrainFull
79    for &'a mut HashMap<K, V, S>
80{
81    type Iter = Drain<'a, K, V>;
82    type Item = (K, V);
83
84    fn par_drain(self) -> Self::Iter {
85        let vec: Vec<_> = self.drain().collect();
86        Drain {
87            inner: vec.into_par_iter(),
88            marker: PhantomData,
89        }
90    }
91}
92
93delegate_iterator! {
94    Drain<'_, K, V> => (K, V),
95    impl<K: Hash + Eq + Send, V: Send>
96}