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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::storage::Storage;
use std::hash;
pub struct MapStorage<K, V> {
inner: hashbrown::HashMap<K, V>,
}
impl<K, V> Clone for MapStorage<K, V>
where
K: Clone,
V: Clone,
{
fn clone(&self) -> Self {
MapStorage {
inner: self.inner.clone(),
}
}
}
impl<K, V> Default for MapStorage<K, V>
where
K: Eq + hash::Hash,
{
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}
impl<K, V> PartialEq for MapStorage<K, V>
where
K: Eq + hash::Hash,
V: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl<K, V> Eq for MapStorage<K, V>
where
K: Eq + hash::Hash,
V: Eq,
{
}
pub struct Iter<K, V> {
iter: std::vec::IntoIter<(K, *const V)>,
}
impl<K, V> Clone for Iter<K, V>
where
K: Copy,
{
fn clone(&self) -> Iter<K, V> {
Iter {
iter: self.iter.clone(),
}
}
}
impl<K, V> Iterator for Iter<K, V> {
type Item = (K, *const V);
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
pub struct IterMut<K, V> {
iter: std::vec::IntoIter<(K, *mut V)>,
}
impl<K, V> Iterator for IterMut<K, V> {
type Item = (K, *mut V);
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
impl<K, V> Storage<K, V> for MapStorage<K, V>
where
K: Copy + Eq + hash::Hash,
{
type Iter = Iter<K, V>;
type IterMut = IterMut<K, V>;
#[inline]
fn insert(&mut self, key: K, value: V) -> Option<V> {
self.inner.insert(key, value)
}
#[inline]
fn get(&self, key: K) -> Option<&V> {
self.inner.get(&key)
}
#[inline]
fn get_mut(&mut self, key: K) -> Option<&mut V> {
self.inner.get_mut(&key)
}
#[inline]
fn remove(&mut self, key: K) -> Option<V> {
self.inner.remove(&key)
}
#[inline]
fn clear(&mut self) {
self.inner.clear();
}
#[inline]
fn iter(&self) -> Self::Iter {
Iter {
iter: self
.inner
.iter()
.map(|(k, v)| (*k, v as *const V))
.collect::<Vec<_>>()
.into_iter(),
}
}
#[inline]
fn iter_mut(&mut self) -> Self::IterMut {
IterMut {
iter: self
.inner
.iter_mut()
.map(|(k, v)| (*k, v as *mut V))
.collect::<Vec<_>>()
.into_iter(),
}
}
}