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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use super::{Iter, IterMut, TypedIndex};
use std::{
marker::PhantomData,
ops::{Index, IndexMut},
};
#[derive(Debug)]
pub struct SliceMap<K, V>
where
K: TypedIndex,
{
_marker: PhantomData<K>,
slice: [V],
}
impl<K, V> SliceMap<K, V>
where
K: TypedIndex,
{
pub fn get(&self, index: K) -> Option<&V> {
self.slice.get(index.index())
}
pub fn get_mut(&mut self, index: K) -> Option<&mut V> {
self.slice.get_mut(index.index())
}
pub fn len(&self) -> usize {
self.slice.len()
}
pub fn iter(&self) -> Iter<K, V> {
Iter::new(self.slice.iter())
}
pub fn iter_mut(&mut self) -> IterMut<K, V> {
IterMut::new(self.slice.iter_mut())
}
pub fn as_ptr(&self) -> *const V {
self as *const SliceMap<K, V> as *const V
}
pub fn as_mut_ptr(&mut self) -> *mut V {
self as *mut SliceMap<K, V> as *mut V
}
}
impl<K, V> Index<K> for SliceMap<K, V>
where
K: TypedIndex,
{
type Output = V;
fn index(&self, index: K) -> &V {
&self.slice[index.index()]
}
}
impl<K, V> IndexMut<K> for SliceMap<K, V>
where
K: TypedIndex,
{
fn index_mut(&mut self, index: K) -> &mut V {
&mut self.slice[index.index()]
}
}