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

/// A reversed slice; see [`Slice::rev`].
pub struct Reverse<S>(pub S);

impl<S> Slice for Reverse<S>
where
    S: Slice,
{
    type Output = S::Output;

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

    fn get_with<F: FnMut(&Self::Output) -> U, U>(&self, index: usize, f: &mut F) -> Option<U> {
        self.0.get_with(self.len() - 1 - index, f)
    }
}

impl<S> SliceOwned for Reverse<S>
where
    S: SliceOwned,
{
    fn get_owned(&self, index: usize) -> Option<Self::Output> {
        self.0.get_owned(self.len() - 1 - index)
    }
}

impl<S> SliceBorrowed for Reverse<S>
where
    S: SliceBorrowed,
{
    fn get(&self, index: usize) -> Option<&Self::Output> {
        self.0.get(self.len() - 1 - index)
    }
}

impl<S> SliceMut for Reverse<S>
where
    S: SliceMut,
{
    fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
        self.0.get_mut(self.len() - 1 - index)
    }
}

// SAFETY: the underlying slice is `Unique`, and reversing cannot alias
unsafe impl<S> Unique for Reverse<S> where S: Unique {}