rayon/collections/
hash_set.rs

1//! This module contains the parallel iterator types for hash sets
2//! (`HashSet<T>`). 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::HashSet;
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 set
15#[derive(Debug)] // std doesn't Clone
16pub struct IntoIter<T: Hash + Eq + Send> {
17    inner: vec::IntoIter<T>,
18}
19
20into_par_vec! {
21    HashSet<T, S> => IntoIter<T>,
22    impl<T: Hash + Eq + Send, S: BuildHasher>
23}
24
25delegate_iterator! {
26    IntoIter<T> => T,
27    impl<T: Hash + Eq + Send>
28}
29
30/// Parallel iterator over an immutable reference to a hash set
31#[derive(Debug)]
32pub struct Iter<'a, T: Hash + Eq + Sync> {
33    inner: vec::IntoIter<&'a T>,
34}
35
36impl<'a, T: Hash + Eq + Sync> Clone for Iter<'a, T> {
37    fn clone(&self) -> Self {
38        Iter {
39            inner: self.inner.clone(),
40        }
41    }
42}
43
44into_par_vec! {
45    &'a HashSet<T, S> => Iter<'a, T>,
46    impl<'a, T: Hash + Eq + Sync, S: BuildHasher>
47}
48
49delegate_iterator! {
50    Iter<'a, T> => &'a T,
51    impl<'a, T: Hash + Eq + Sync + 'a>
52}
53
54// `HashSet` doesn't have a mutable `Iterator`
55
56/// Draining parallel iterator that moves out of a hash set,
57/// but keeps the total capacity.
58#[derive(Debug)]
59pub struct Drain<'a, T: Hash + Eq + Send> {
60    inner: vec::IntoIter<T>,
61    marker: PhantomData<&'a mut HashSet<T>>,
62}
63
64impl<'a, T: Hash + Eq + Send, S: BuildHasher> ParallelDrainFull for &'a mut HashSet<T, S> {
65    type Iter = Drain<'a, T>;
66    type Item = T;
67
68    fn par_drain(self) -> Self::Iter {
69        let vec: Vec<_> = self.drain().collect();
70        Drain {
71            inner: vec.into_par_iter(),
72            marker: PhantomData,
73        }
74    }
75}
76
77delegate_iterator! {
78    Drain<'_, T> => T,
79    impl<T: Hash + Eq + Send>
80}