use super::{ExactParallelSink, FromExactParallelSink};
use crate::iter::source::zip::or_bools;
use std::array;
use std::mem::ManuallyDrop;
#[must_use = "iterator adaptors are lazy"]
pub struct UnzipSink<T>(T);
#[must_use = "iterator adaptors are lazy"]
pub struct Unzip<T>(pub(crate) T);
struct SkipItemGuard<'a, S: ExactParallelSink> {
sink: &'a S,
index: usize,
}
impl<'a, S: ExactParallelSink> SkipItemGuard<'a, S> {
fn into_inner(self) -> &'a S {
let this = ManuallyDrop::new(self);
this.sink
}
}
impl<S: ExactParallelSink> Drop for SkipItemGuard<'_, S> {
fn drop(&mut self) {
unsafe {
self.sink.skip_item_range(self.index..self.index + 1);
}
}
}
macro_rules! unzipable_tuple {
( $($tuple:ident $i:tt),+ ) => {
impl<$($tuple,)+> ExactParallelSink for UnzipSink<($($tuple,)+)>
where $($tuple: ExactParallelSink,)+ {
type Item = ( $($tuple::Item,)+ );
const NEEDS_CLEANUP: bool = or_bools!( $($tuple::NEEDS_CLEANUP),+ );
fn new(len: usize) -> Self {
Self( ( $($tuple::new(len),)+ ) )
}
unsafe fn push_item(&self, index: usize, item: Self::Item) {
let guards = ( $(
SkipItemGuard {
sink: &self.0.$i,
index,
},
)+ );
$( unsafe {
guards.$i.into_inner().push_item(index, item.$i);
} )+
}
unsafe fn skip_item_range(&self, range: std::ops::Range<usize>) {
let _ = ( $(
scopeguard::guard(&self.0.$i, |sink| unsafe {
sink.skip_item_range(range.clone());
}),
)+ );
}
unsafe fn cancel(self) {
let _ = ( $(
scopeguard::guard(self.0.$i, |sink| unsafe {
sink.cancel();
}),
)+ );
}
}
impl<$($tuple,)+> FromExactParallelSink for Unzip<($($tuple,)+)>
where $($tuple: FromExactParallelSink,)+ {
type Item = ( $($tuple::Item,)+ );
type Sink = UnzipSink<( $($tuple::Sink,)+ )>;
unsafe fn finalize(sink: Self::Sink) -> Self {
Unzip(( $( unsafe {
$tuple::finalize(sink.0.$i)
} ,)+ ))
}
}
}
}
unzipable_tuple!(A 0);
unzipable_tuple!(A 0, B 1);
unzipable_tuple!(A 0, B 1, C 2);
unzipable_tuple!(A 0, B 1, C 2, D 3);
unzipable_tuple!(A 0, B 1, C 2, D 3, E 4);
unzipable_tuple!(A 0, B 1, C 2, D 3, E 4, F 5);
unzipable_tuple!(A 0, B 1, C 2, D 3, E 4, F 5, G 6);
unzipable_tuple!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7);
unzipable_tuple!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8);
unzipable_tuple!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9);
unzipable_tuple!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10);
unzipable_tuple!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11);
impl<T, const N: usize> ExactParallelSink for UnzipSink<[T; N]>
where
T: ExactParallelSink,
{
type Item = [T::Item; N];
const NEEDS_CLEANUP: bool = T::NEEDS_CLEANUP;
fn new(len: usize) -> Self {
Self(array::from_fn(|_| T::new(len)))
}
unsafe fn push_item(&self, index: usize, item: Self::Item) {
let mut i = 0;
let guarded = item.map(|item| {
let sink = &self.0[i];
i += 1;
(item, SkipItemGuard { sink, index })
});
let _: [(); N] = guarded.map(|(item, guard)| {
unsafe {
guard.into_inner().push_item(index, item);
}
});
}
unsafe fn skip_item_range(&self, range: std::ops::Range<usize>) {
let _ = self.0.each_ref().map(|sink| {
scopeguard::guard(sink, |sink| unsafe {
sink.skip_item_range(range.clone());
})
});
}
unsafe fn cancel(self) {
let _ = self.0.map(|sink| {
scopeguard::guard(sink, |sink| unsafe {
sink.cancel();
})
});
}
}
impl<T, const N: usize> FromExactParallelSink for Unzip<[T; N]>
where
T: FromExactParallelSink,
{
type Item = [T::Item; N];
type Sink = UnzipSink<[T::Sink; N]>;
unsafe fn finalize(sink: Self::Sink) -> Self {
Unzip(sink.0.map(|sink| unsafe { T::finalize(sink) }))
}
}