Skip to main content

peggen_impl/
rvec.rs

1mod peggen {
2    pub(crate) use peggen_core::*;
3    pub(crate) use peggen_macs::*;    
4}
5
6use core::fmt::Debug;
7use alloc::vec::Vec;
8use peggen::*;
9
10/// Reversed vector
11#[derive(PrependAstImpl, Clone, PartialEq, Eq)]
12pub struct RVec<T>(Vec<T>);
13
14impl<T, Extra: Copy> Prepend<Extra> for RVec<T> {
15    type Item = T;
16    fn empty(_: Extra) -> Self {
17        Self(Vec::new())
18    }
19    fn prepend(&mut self, value: Self::Item, _: Extra) {
20        let Self(inner) = self;
21        inner.push(value);
22    }
23}
24
25impl<T: Debug> Debug for RVec<T> {
26    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27        f.debug_list().entries(self.0.iter().rev()).finish()
28    }
29}
30
31impl<T> IntoIterator for RVec<T> {
32    type Item = T;
33    type IntoIter = core::iter::Rev<alloc::vec::IntoIter<T>>;
34    fn into_iter(self) -> Self::IntoIter {
35        let Self(inner) = self;
36        inner.into_iter().rev()
37    }
38}
39
40impl<T> core::ops::Index<usize> for RVec<T> {
41    type Output = T;
42    fn index(&self, index: usize) -> &Self::Output {
43        &self.0[self.0.len()-1-index]
44    }
45}