#[cfg(test)]
mod tests;
use std::thread;
const MAX_REPORT_WORKERS: usize = 8;
pub fn collect_ordered_reports<T: Sync, R: Send>(
items: &[T],
query: impl Fn(&T) -> R + Sync,
worker_panic: impl Fn(&T) -> R,
) -> Vec<R> {
let mut reports = Vec::with_capacity(items.len());
for batch in items.chunks(MAX_REPORT_WORKERS) {
reports.extend(thread::scope(|scope| {
let handles = batch
.iter()
.map(|item| (item, scope.spawn(|| query(item))))
.collect::<Vec<_>>();
handles
.into_iter()
.map(|(item, handle)| handle.join().unwrap_or_else(|_| worker_panic(item)))
.collect::<Vec<_>>()
}));
}
reports
}