kul/parser/
arc_alloc.rs

1use std::marker::PhantomData;
2
3use crate::{
4    parser::{DatumAllocator, AllocError},
5    datum::{DatumArc, ArcDatum},
6    Text,
7};
8
9
10/// A [`DatumAllocator`] for generic `Datum` types which allocates `Datum`
11/// values in heap-allocated `Arc`s.
12///
13/// [`DatumAllocator`]: ../../kul_core/parser/trait.DatumAllocator.html
14#[derive(Debug)]
15pub struct ArcDatumAllocator<TT, ET>(PhantomData<(*const TT, *const ET)>);
16
17/// Must implement this manually because deriving would place unwanted bounds on
18/// the type parameters.
19impl<TT, ET> Default for ArcDatumAllocator<TT, ET>
20    where TT: Text,
21{
22    /// This type only has one, default, value.
23    #[inline]
24    fn default() -> Self {
25        Self(PhantomData)
26    }
27}
28
29impl<TT, ET> DatumAllocator for ArcDatumAllocator<TT, ET>
30    where TT: Text,
31{
32    type TT = TT;
33    type ET = ET;
34    type DR = DatumArc<Self::TT, Self::ET>;
35
36    #[inline]
37    fn new_datum(&mut self, from: ArcDatum<Self::TT, Self::ET>)
38                 -> Result<Self::DR, AllocError>
39    {
40        Ok(DatumArc::new(from))
41    }
42}
43
44
45// Note: Tested by the arc_alloc integration test.