slice_utils/
zip.rs

1use crate::{Slice, SliceOwned, Unique};
2
3/// Two slices zipped together; see [`SliceOwned::zip`].
4#[derive(Clone, Copy, Hash)]
5pub struct Zip<S1, S2>(pub S1, pub S2);
6
7// TODO: can this be less strict?
8impl<S1, S2> Slice for Zip<S1, S2>
9where
10    S1: SliceOwned,
11    S2: SliceOwned,
12{
13    type Output = (S1::Output, S2::Output);
14
15    fn len(&self) -> usize {
16        self.0.len().min(self.1.len())
17    }
18
19    fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
20        Some(f(&(self.0.get_owned(index)?, self.1.get_owned(index)?)))
21    }
22}
23
24impl<S1, S2> SliceOwned for Zip<S1, S2>
25where
26    S1: SliceOwned,
27    S2: SliceOwned,
28{
29    fn get_owned(&self, index: usize) -> Option<Self::Output> {
30        Some((self.0.get_owned(index)?, self.1.get_owned(index)?))
31    }
32}
33
34// SAFETY: both underlying slices are `Unique`
35unsafe impl<S1, S2> Unique for Zip<S1, S2>
36where
37    S1: Unique,
38    S2: Unique,
39{
40}