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