1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
use super::{IntoParallelIterator, ParallelIterator};
use super::chain::ChainIter;
use super::slice::SliceIter;
use super::slice_mut::SliceIterMut;
use super::vec::VecIter;
use std::collections::{BinaryHeap, BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
use std::iter::FromIterator;

// This could almost be replaced with a blanket implementation for *all*
// `IntoIterator` types, but we'd need to specialize a few like `Vec`.
macro_rules! vectorized {
    (@impl_body) => {
        type Item = <Self as IntoIterator>::Item;
        type Iter = VecIter<Self::Item>;

        fn into_par_iter(self) -> Self::Iter {
            Vec::from_iter(self).into_par_iter()
        }
    };
    ( impl<'a, $($c:ident),*> IntoParallelIterator for &'a mut $t:ty  ) => {
        impl<'a, $($c),*> IntoParallelIterator for &'a mut $t
            where &'a mut $t: IntoIterator,
                  <&'a mut $t as IntoIterator>::Item: Send
        {
            vectorized!{@impl_body}
        }
    };
    ( impl<'a, $($c:ident),*> IntoParallelIterator for &'a $t:ty  ) => {
        impl<'a, $($c),*> IntoParallelIterator for &'a $t
            where &'a $t: IntoIterator,
                  <&'a $t as IntoIterator>::Item: Send
        {
            vectorized!{@impl_body}
        }
    };
    ( impl<$($c:ident),*> IntoParallelIterator for $t:ty  ) => {
        impl<$($c),*> IntoParallelIterator for $t
            where $t: IntoIterator,
                  <$t as IntoIterator>::Item: Send
        {
            vectorized!{@impl_body}
        }
    };
}


impl<T: Send> IntoParallelIterator for BinaryHeap<T> {
    type Item = T;
    type Iter = VecIter<T>;

    fn into_par_iter(self) -> Self::Iter {
        Vec::from(self).into_par_iter()
    }
}

vectorized!{ impl<'a, T> IntoParallelIterator for &'a BinaryHeap<T> }
// `BinaryHeap` doesn't have a mutable `Iterator`


vectorized!{ impl<K, V> IntoParallelIterator for BTreeMap<K, V> }
vectorized!{ impl<'a, K, V> IntoParallelIterator for &'a BTreeMap<K, V> }
vectorized!{ impl<'a, K, V> IntoParallelIterator for &'a mut BTreeMap<K, V> }


vectorized!{ impl<T> IntoParallelIterator for BTreeSet<T> }
vectorized!{ impl<'a, T> IntoParallelIterator for &'a BTreeSet<T> }
// `BTreeSet` doesn't have a mutable `Iterator`


vectorized!{ impl<K, V, S> IntoParallelIterator for HashMap<K, V, S> }
vectorized!{ impl<'a, K, V, S> IntoParallelIterator for &'a HashMap<K, V, S> }
vectorized!{ impl<'a, K, V, S> IntoParallelIterator for &'a mut HashMap<K, V, S> }


vectorized!{ impl<T, S> IntoParallelIterator for HashSet<T, S> }
vectorized!{ impl<'a, T, S> IntoParallelIterator for &'a HashSet<T, S> }
// `HashSet` doesn't have a mutable `Iterator`


vectorized!{ impl<T> IntoParallelIterator for LinkedList<T> }
vectorized!{ impl<'a, T> IntoParallelIterator for &'a LinkedList<T> }
vectorized!{ impl<'a, T> IntoParallelIterator for &'a mut LinkedList<T> }


vectorized!{ impl<T> IntoParallelIterator for VecDeque<T> }

impl<'a, T: Sync> IntoParallelIterator for &'a VecDeque<T> {
    type Item = &'a T;
    type Iter = ChainIter<SliceIter<'a, T>, SliceIter<'a, T>>;

    fn into_par_iter(self) -> Self::Iter {
        let (a, b) = self.as_slices();
        a.into_par_iter().chain(b)
    }
}

impl<'a, T: Send> IntoParallelIterator for &'a mut VecDeque<T> {
    type Item = &'a mut T;
    type Iter = ChainIter<SliceIterMut<'a, T>, SliceIterMut<'a, T>>;

    fn into_par_iter(self) -> Self::Iter {
        let (a, b) = self.as_mut_slices();
        a.into_par_iter().chain(b)
    }
}