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
use crate::{Slice, SliceBorrowed, SliceMut, SliceOwned};

/// Two chained slices; see [`Slice::chain`].
pub struct Chain<S1, S2>(pub S1, pub S2);

impl<S1, S2> Slice for Chain<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> {
        self.0
            .get_with(index, f)
            .or_else(|| self.1.get_with(index - self.0.len(), f))
    }
}

impl<S1, S2> SliceOwned for Chain<S1, S2>
where
    S1: SliceOwned,
    S2: SliceOwned<Output = S1::Output>,
{
    fn get_owned(&self, index: usize) -> Option<Self::Output> {
        self.0
            .get_owned(index)
            .or_else(|| self.1.get_owned(index - self.0.len()))
    }
}

impl<S1, S2> SliceBorrowed for Chain<S1, S2>
where
    S1: SliceBorrowed,
    S2: SliceBorrowed<Output = S1::Output>,
{
    fn get(&self, index: usize) -> Option<&Self::Output> {
        self.0
            .get(index)
            .or_else(|| self.1.get(index - self.0.len()))
    }
}

impl<S1, S2> SliceMut for Chain<S1, S2>
where
    S1: SliceMut,
    S2: SliceMut<Output = S1::Output>,
{
    fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
        let offset = self.0.len();
        if let r @ Some(_) = self.0.get_mut(index) {
            r
        } else {
            self.1.get_mut(index - offset)
        }
    }
}