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