1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::{Slice, SliceOwned};

/// Two slices zipped together; see [`SliceOwned::zip`].
#[derive(Clone, Copy, Hash)]
pub struct Zip<S1, S2>(pub S1, pub S2);

// TODO: can this be less strict?
impl<S1, S2> Slice for Zip<S1, S2>
where
    S1: SliceOwned,
    S2: SliceOwned,
{
    type Output = (S1::Output, S2::Output);

    fn len(&self) -> usize {
        self.0.len().min(self.1.len())
    }

    fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
        Some(f(&(self.0.get_owned(index)?, self.1.get_owned(index)?)))
    }
}

impl<S1, S2> SliceOwned for Zip<S1, S2>
where
    S1: SliceOwned,
    S2: SliceOwned,
{
    fn get_owned(&self, index: usize) -> Option<Self::Output> {
        Some((self.0.get_owned(index)?, self.1.get_owned(index)?))
    }
}