1use core::ops::DerefMut;
4
5use crate::index::{Index, Length, TreeIndex};
6use crate::pointer::{Pointer, Width};
7
8pub trait Storage<T>
10where
11 Self: Sized + DerefMut<Target = [T]>,
12{
13 type Error: 'static;
15
16 const EMPTY: Self;
18
19 fn with_capacity(capacity: usize) -> Result<Self, Self::Error>;
21
22 fn capacity(&self) -> usize;
24
25 fn push(&mut self, item: T) -> Result<(), Self::Error>;
27}
28
29#[cfg(feature = "alloc")]
30impl<T> Storage<T> for alloc::vec::Vec<T> {
31 type Error = core::convert::Infallible;
32
33 const EMPTY: Self = alloc::vec::Vec::new();
34
35 #[inline]
36 fn with_capacity(capacity: usize) -> Result<Self, Self::Error> {
37 Ok(alloc::vec::Vec::with_capacity(capacity))
38 }
39
40 #[inline]
41 fn capacity(&self) -> usize {
42 alloc::vec::Vec::capacity(self)
43 }
44
45 #[inline]
46 fn push(&mut self, item: T) -> Result<(), Self::Error> {
47 alloc::vec::Vec::push(self, item);
48 Ok(())
49 }
50}
51
52#[macro_export]
78macro_rules! flavor {
79 (
80 $(#[doc = $doc:literal])*
81 $vis:vis struct $ty:ident {
82 type Index = $index:ty;
83 $(type Width = $width:ty;)?
84 $(type Storage = $storage:ty;)?
85 $(type Indexes = $indexes:ty;)?
86 }
87 ) => {
88 $(#[doc = $doc])*
89 #[non_exhaustive]
90 $vis struct $ty;
91
92 impl $crate::Flavor for $ty {
93 type Error = core::convert::Infallible;
94 type Index = $index;
95 type Length = <$index as $crate::Index>::Length;
96 type Width = $crate::flavor!(@width $($width)*);
97 type Pointer = $crate::flavor!(@pointer $($width)*);
98 type Storage<T> = $crate::macro_support::Vec<T>;
99 type Indexes = $crate::flavor!(@indexes $($indexes)*);
100 }
101 };
102
103 (@width $ty:ty) => { $ty };
104 (@width) => { usize };
105 (@pointer $ty:ty) => { <$ty as $crate::pointer::Width>::Pointer };
106 (@pointer) => { <usize as $crate::pointer::Width>::Pointer };
107 (@indexes $ty:ty) => { $ty };
108 (@indexes) => { $crate::macro_support::DefaultIndexes<Self> };
109}
110
111flavor! {
112 pub struct FlavorDefault {
116 type Index = u32;
117 type Width = usize;
118 }
119}
120
121pub trait Flavor {
134 type Error;
136 type Index: Index<Length = Self::Length>;
138 type Length: Length;
140 type Width: Width<Pointer = Self::Pointer>;
142 type Pointer: Pointer;
144 type Storage<T>: Storage<T, Error = Self::Error>;
146 type Indexes: Storage<TreeIndex<Self>, Error = Self::Error>;
148}