slice_utils/
reverse.rs

1use crate::{Slice, SliceBorrowed, SliceMut, SliceOwned, Unique};
2
3/// A reversed slice; see [`Slice::rev`].
4pub struct Reverse<S>(pub S);
5
6impl<S> Slice for Reverse<S>
7where
8    S: Slice,
9{
10    type Output = S::Output;
11
12    fn len(&self) -> usize {
13        self.0.len()
14    }
15
16    fn get_with<F: FnMut(&Self::Output) -> U, U>(&self, index: usize, f: &mut F) -> Option<U> {
17        self.0.get_with(self.len() - 1 - index, f)
18    }
19}
20
21impl<S> SliceOwned for Reverse<S>
22where
23    S: SliceOwned,
24{
25    fn get_owned(&self, index: usize) -> Option<Self::Output> {
26        self.0.get_owned(self.len() - 1 - index)
27    }
28}
29
30impl<S> SliceBorrowed for Reverse<S>
31where
32    S: SliceBorrowed,
33{
34    fn get(&self, index: usize) -> Option<&Self::Output> {
35        self.0.get(self.len() - 1 - index)
36    }
37}
38
39impl<S> SliceMut for Reverse<S>
40where
41    S: SliceMut,
42{
43    fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
44        self.0.get_mut(self.len() - 1 - index)
45    }
46}
47
48// SAFETY: the underlying slice is `Unique`, and reversing cannot alias
49unsafe impl<S> Unique for Reverse<S> where S: Unique {}