//! Parallelism utilities. use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator}; /// `Last` is a helper to extract the last value of a [`ParallelIterator`]. /// /// It can be used with [`ParallelIterator::collect`], [`ParallelIterator::unzip`], and similar /// methods. pub struct Last { result: Option, } impl Last { /// Extract the collected value. pub fn into_inner(self) -> Option { self.result } } impl Default for Last { fn default() -> Self { Self { result: None } } } impl FromParallelIterator for Last { fn from_par_iter(par_iter: I) -> Self where I: IntoParallelIterator, { let mut last = Self::default(); last.par_extend(par_iter); last } } impl ParallelExtend for Last { fn par_extend(&mut self, par_iter: I) where I: IntoParallelIterator, { // The find_last implementation does a bunch of bookkeeping to short-circuit once it finds // the most-last match, so rely on that here. self.result = par_iter.into_par_iter().find_last(|_| true) } }