rune/grammar/
flavor.rs

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
use core::ops::{Deref, DerefMut};

use syntree::{Index, Storage, TreeIndex, Width};

use crate::alloc::{self, Vec};

pub(crate) struct Flavor;

pub struct AllocVec<T> {
    inner: Vec<T>,
}

impl<T> Deref for AllocVec<T> {
    type Target = [T];

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T> DerefMut for AllocVec<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl<T> Storage<T> for AllocVec<T> {
    const EMPTY: Self = Self { inner: Vec::new() };

    type Error = alloc::Error;

    #[inline]
    fn with_capacity(capacity: usize) -> alloc::Result<Self> {
        Ok(Self {
            inner: Vec::try_with_capacity(capacity)?,
        })
    }

    #[inline]
    fn capacity(&self) -> usize {
        self.inner.capacity()
    }

    #[inline]
    fn push(&mut self, item: T) -> alloc::Result<()> {
        self.inner.try_push(item)
    }
}

impl syntree::Flavor for Flavor {
    type Error = alloc::Error;
    type Index = u32;
    type Length = <u32 as Index>::Length;
    type Width = usize;
    type Pointer = <usize as Width>::Pointer;
    type Storage<T> = AllocVec<T>;
    type Indexes = AllocVec<TreeIndex<Self>>;
}