static_atom/
lib.rs

1#![deny(warnings)]
2
3pub mod iterators;
4
5use std::iter::FromIterator;
6use std::mem;
7
8use crate::iterators::{Iter, IterMut, Keys, Values};
9
10pub use try_from::TryFrom;
11
12pub trait AtomMap: FromIterator<(<Self as AtomMap>::Key, <Self as AtomMap>::Value)> {
13    type Key: TryFrom<usize>;
14    type Value;
15
16    fn entry(&self, key: Self::Key) -> &Option<Self::Value>;
17    fn entry_mut(&mut self, key: Self::Key) -> &mut Option<Self::Value>;
18    fn entries(&self) -> &[Option<Self::Value>];
19    fn entries_mut(&mut self) -> &mut [Option<Self::Value>];
20
21    fn get(&self, key: Self::Key) -> Option<&Self::Value> {
22        self.entry(key).as_ref()
23    }
24
25    fn get_mut(&mut self, key: Self::Key) -> Option<&mut Self::Value> {
26        self.entry_mut(key).as_mut()
27    }
28
29    fn insert(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value> {
30        mem::replace(self.entry_mut(key), Some(value))
31    }
32
33    fn remove(&mut self, key: Self::Key) -> Option<Self::Value> {
34        mem::replace(self.entry_mut(key), None)
35    }
36
37    fn get_or_insert(&mut self, key: Self::Key, value: Self::Value) -> &mut Self::Value {
38        let entry = self.entry_mut(key);
39        if entry.is_none() {
40            *entry = Some(value);
41        }
42
43        entry.as_mut().unwrap()
44    }
45
46    fn get_or_insert_with<F: FnOnce() -> Self::Value>(&mut self, key: Self::Key, f: F) -> &mut Self::Value {
47        let entry = self.entry_mut(key);
48        if entry.is_none() {
49            *entry = Some(f());
50        }
51
52        entry.as_mut().unwrap()
53    }
54
55    fn iter(&self) -> Iter<Self::Key, Self::Value> {
56        Iter::new(self.entries())
57    }
58
59    fn iter_mut(&mut self) -> IterMut<Self::Key, Self::Value> {
60        IterMut::new(self.entries_mut())
61    }
62
63    fn keys(&self) -> Keys<Self::Key, Self::Value> {
64        Keys::new(self.entries())
65    }
66
67    fn values(&self) -> iterators::Values<Self::Value> {
68        Values::new(self.entries())
69    }
70}
71
72pub trait Mapping<Atom> {
73    type Value;
74}
75
76pub trait TypedAtomMap<M> {
77    fn entry<A>(&self) -> &Option<<M as Mapping<A>>::Value>
78    where
79        M: Mapping<A>,
80        A: 'static;
81
82    fn entry_mut<A>(&mut self) -> &mut Option<<M as Mapping<A>>::Value>
83    where
84        M: Mapping<A>,
85        A: 'static;
86
87    fn get<A>(&self) -> Option<&<M as Mapping<A>>::Value>
88    where
89        M: Mapping<A>,
90        A: 'static,
91    {
92        self.entry::<A>().as_ref()
93    }
94
95    fn get_mut<A>(&mut self) -> Option<&mut <M as Mapping<A>>::Value>
96    where
97        M: Mapping<A>,
98        A: 'static,
99    {
100        self.entry_mut::<A>().as_mut()
101    }
102
103    fn insert<A>(&mut self, value: <M as Mapping<A>>::Value) -> Option<<M as Mapping<A>>::Value>
104    where
105        M: Mapping<A>,
106        A: 'static,
107    {
108        mem::replace(self.entry_mut::<A>(), Some(value))
109    }
110
111    fn remove<A>(&mut self) -> Option<<M as Mapping<A>>::Value>
112    where
113        M: Mapping<A>,
114        A: 'static,
115    {
116        mem::replace(self.entry_mut::<A>(), None)
117    }
118
119    fn get_or_insert<A>(&mut self, value: <M as Mapping<A>>::Value) -> &mut <M as Mapping<A>>::Value
120    where
121        M: Mapping<A>,
122        A: 'static,
123    {
124        let entry = self.entry_mut::<A>();
125        if entry.is_none() {
126            *entry = Some(value);
127        }
128
129        entry.as_mut().unwrap()
130    }
131
132    fn get_or_insert_with<A, F>(&mut self, f: F) -> &mut <M as Mapping<A>>::Value
133    where
134        M: Mapping<A>,
135        A: 'static,
136        F: FnOnce() -> <M as Mapping<A>>::Value,
137    {
138        let entry = self.entry_mut::<A>();
139        if entry.is_none() {
140            *entry = Some(f());
141        }
142
143        entry.as_mut().unwrap()
144    }
145}
146
147pub trait Expect<T>: Sized {
148    fn expect(self, value: &T) -> Option<Self>;
149}
150
151impl<'a, T: PartialEq> Expect<T> for &'a [T] {
152    #[inline]
153    fn expect(self, value: &T) -> Option<Self> {
154        if let Some(b) = self.first() {
155            if b == value {
156                return Some(&self[1..]);
157            }
158        }
159
160        None
161    }
162}
163
164impl<'a> Expect<[u8; 1]> for &'a [u8] {
165    #[inline]
166    fn expect(self, a: &[u8; 1]) -> Option<Self> {
167        self.expect(&a[0])
168    }
169}
170
171impl<'a> Expect<[u8; 2]> for &'a [u8] {
172    #[inline]
173    fn expect(self, a: &[u8; 2]) -> Option<Self> {
174        if self.len() < 2 {
175            return None;
176        }
177
178        let s2 = unsafe { mem::transmute::<&[u8], &[u16]>(self) };
179        let a2 = unsafe { mem::transmute::<[u8; 2], u16>(*a) };
180        s2.expect(&a2)?;
181        Some(&self[2..])
182    }
183}
184
185impl<'a> Expect<[u8; 3]> for &'a [u8] {
186    #[inline]
187    fn expect(self, a: &[u8; 3]) -> Option<Self> {
188        self.expect(&[a[0], a[1]])?.expect(&[a[2]])
189    }
190}
191
192impl<'a> Expect<[u8; 4]> for &'a [u8] {
193    #[inline]
194    fn expect(self, a: &[u8; 4]) -> Option<Self> {
195        if self.len() < 4 {
196            return None;
197        }
198
199        let s4 = unsafe { mem::transmute::<&[u8], &[u32]>(self) };
200        let a4 = unsafe { mem::transmute::<[u8; 4], u32>(*a) };
201        s4.expect(&a4)?;
202        Some(&self[4..])
203    }
204}
205
206impl<'a> Expect<[u8; 5]> for &'a [u8] {
207    #[inline]
208    fn expect(self, a: &[u8; 5]) -> Option<Self> {
209        self.expect(&[a[0], a[1], a[2], a[3]])?.expect(&[a[4]])
210    }
211}
212
213impl<'a> Expect<[u8; 6]> for &'a [u8] {
214    #[inline]
215    fn expect(self, a: &[u8; 6]) -> Option<Self> {
216        self.expect(&[a[0], a[1], a[2], a[3]])?.expect(&[a[4], a[5]])
217    }
218}
219
220impl<'a> Expect<[u8; 7]> for &'a [u8] {
221    #[inline]
222    fn expect(self, a: &[u8; 7]) -> Option<Self> {
223        self.expect(&[a[0], a[1], a[2], a[3]])?
224            .expect(&[a[4], a[5]])?
225            .expect(&[a[6]])
226    }
227}
228
229impl<'a> Expect<[u8; 8]> for &'a [u8] {
230    #[inline]
231    fn expect(self, a: &[u8; 8]) -> Option<Self> {
232        if self.len() < 8 {
233            return None;
234        }
235
236        let s8 = unsafe { mem::transmute::<&[u8], &[u64]>(self) };
237        let a8 = unsafe { mem::transmute::<[u8; 8], u64>(*a) };
238        s8.expect(&a8)?;
239        Some(&self[8..])
240    }
241}