//! Parallel-to-serial iterator bridge use rayon::prelude::*; /// Parallel-to-serial iterator bridge trait /// /// Proposed in and copied from pub trait SerBridge where T: Send + 'static, Self: ParallelIterator + 'static, { fn ser_bridge(self) -> SerBridgeImpl { SerBridgeImpl::new(self) } } impl SerBridge for PI where T: Send + 'static, PI: ParallelIterator + 'static, { } /// Parallel-to-serial iterator bridge /// /// Proposed in and copied from pub struct SerBridgeImpl { rx: crossbeam_channel::Receiver, } impl SerBridgeImpl { pub fn new(par_iterable: impl IntoParallelIterator) -> Self where PI: ParallelIterator + 'static, { let par_iter = par_iterable.into_par_iter(); let (tx, rx) = crossbeam_channel::bounded(0); std::thread::spawn(move || { let _ = par_iter.try_for_each(|item| tx.send(item)); }); SerBridgeImpl { rx } } } impl Iterator for SerBridgeImpl { type Item = T; fn next(&mut self) -> Option { self.rx.recv().ok() } }