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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::{Slice, SliceBorrowed, SliceMut, SliceOwned};

/// Two interleaved slices; see [`Slice::interleave`].
pub struct Interleave<S1, S2>(pub S1, pub S2);

impl<S1, S2> Slice for Interleave<S1, S2>
where
    S1: Slice,
    S2: Slice<Output = S1::Output>,
{
    type Output = S1::Output;

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

    fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
        if index % 2 == 0 {
            self.0.get_with(index / 2, f)
        } else {
            self.1.get_with(index / 2, f)
        }
    }
}

impl<S1, S2> SliceOwned for Interleave<S1, S2>
where
    S1: SliceOwned,
    S2: SliceOwned<Output = S1::Output>,
{
    fn get_owned(&self, index: usize) -> Option<Self::Output> {
        if index % 2 == 0 {
            self.0.get_owned(index / 2)
        } else {
            self.1.get_owned(index / 2)
        }
    }
}

impl<S1, S2> SliceBorrowed for Interleave<S1, S2>
where
    S1: SliceBorrowed,
    S2: SliceBorrowed<Output = S1::Output>,
{
    fn get(&self, index: usize) -> Option<&Self::Output> {
        if index % 2 == 0 {
            self.0.get(index / 2)
        } else {
            self.1.get(index / 2)
        }
    }
}

impl<S1, S2> SliceMut for Interleave<S1, S2>
where
    S1: SliceMut,
    S2: SliceMut<Output = S1::Output>,
{
    fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
        if index % 2 == 0 {
            self.0.get_mut(index / 2)
        } else {
            self.1.get_mut(index / 2)
        }
    }
}