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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#![deny(missing_docs, warnings)]
extern crate unsafe_any as uany;
use uany::{UnsafeAny, UnsafeAnyExt};
use std::any::{Any, TypeId};
use std::collections::{hash_map, HashMap};
use std::marker::PhantomData;
use Entry::{Occupied, Vacant};
pub use internals::{CloneAny, DebugAny};
use internals::Implements;
#[derive(Default, Debug)]
pub struct TypeMap<A: ?Sized = UnsafeAny>
where A: UnsafeAnyExt {
data: HashMap<TypeId, Box<A>>
}
impl<A: ?Sized> Clone for TypeMap<A>
where A: UnsafeAnyExt, Box<A>: Clone {
fn clone(&self) -> TypeMap<A> {
TypeMap { data: self.data.clone() }
}
}
pub type SendMap = TypeMap<UnsafeAny + Send>;
pub type SyncMap = TypeMap<UnsafeAny + Sync>;
pub type ShareMap = TypeMap<UnsafeAny + Send + Sync>;
pub type CloneMap = TypeMap<CloneAny>;
pub type ShareCloneMap = TypeMap<CloneAny + Send + Sync>;
pub type DebugMap = TypeMap<DebugAny>;
pub type ShareDebugMap = TypeMap<DebugAny + Send + Sync>;
fn _assert_types() {
use std::fmt::Debug;
fn _assert_send<T: Send>() { }
fn _assert_sync<T: Sync>() { }
fn _assert_clone<T: Clone>() { }
fn _assert_debug<T: Debug>() { }
_assert_send::<SendMap>();
_assert_sync::<SyncMap>();
_assert_send::<ShareMap>();
_assert_sync::<ShareMap>();
_assert_clone::<CloneMap>();
_assert_debug::<DebugMap>();
}
pub trait Key: Any {
type Value: Any;
}
impl TypeMap {
pub fn new() -> TypeMap {
TypeMap::custom()
}
}
impl<A: UnsafeAnyExt + ?Sized> TypeMap<A> {
pub fn custom() -> TypeMap<A> {
TypeMap {
data: HashMap::new()
}
}
pub fn insert<K: Key>(&mut self, val: K::Value) -> Option<K::Value>
where K::Value: Any + Implements<A> {
self.data.insert(TypeId::of::<K>(), val.into_object()).map(|v| unsafe {
*v.downcast_unchecked::<K::Value>()
})
}
pub fn get<K: Key>(&self) -> Option<&K::Value>
where K::Value: Any + Implements<A> {
self.data.get(&TypeId::of::<K>()).map(|v| unsafe {
v.downcast_ref_unchecked::<K::Value>()
})
}
pub fn get_mut<K: Key>(&mut self) -> Option<&mut K::Value>
where K::Value: Any + Implements<A> {
self.data.get_mut(&TypeId::of::<K>()).map(|v| unsafe {
v.downcast_mut_unchecked::<K::Value>()
})
}
pub fn contains<K: Key>(&self) -> bool {
self.data.contains_key(&TypeId::of::<K>())
}
pub fn remove<K: Key>(&mut self) -> Option<K::Value>
where K::Value: Any + Implements<A> {
self.data.remove(&TypeId::of::<K>()).map(|v| unsafe {
*v.downcast_unchecked::<K::Value>()
})
}
pub fn entry<'a, K: Key>(&'a mut self) -> Entry<'a, K, A>
where K::Value: Any + Implements<A> {
match self.data.entry(TypeId::of::<K>()) {
hash_map::Entry::Occupied(e) => Occupied(OccupiedEntry { data: e, _marker: PhantomData }),
hash_map::Entry::Vacant(e) => Vacant(VacantEntry { data: e, _marker: PhantomData })
}
}
pub unsafe fn data(&self) -> &HashMap<TypeId, Box<A>> {
&self.data
}
pub unsafe fn data_mut(&mut self) -> &mut HashMap<TypeId, Box<A>> {
&mut self.data
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn clear(&mut self) {
self.data.clear()
}
}
pub enum Entry<'a, K, A: ?Sized + UnsafeAnyExt + 'a = UnsafeAny> {
Occupied(OccupiedEntry<'a, K, A>),
Vacant(VacantEntry<'a, K, A>)
}
impl<'a, K: Key, A: ?Sized + UnsafeAnyExt + 'a = UnsafeAny> Entry<'a, K, A> {
pub fn or_insert(self, default: K::Value) -> &'a mut K::Value
where K::Value: Any + Implements<A> {
match self {
Entry::Occupied(inner) => inner.into_mut(),
Entry::Vacant(inner) => inner.insert(default),
}
}
pub fn or_insert_with<F: FnOnce() -> K::Value>(self, default: F) -> &'a mut K::Value
where K::Value: Any + Implements<A> {
match self {
Entry::Occupied(inner) => inner.into_mut(),
Entry::Vacant(inner) => inner.insert(default()),
}
}
}
pub struct OccupiedEntry<'a, K, A: ?Sized + UnsafeAnyExt + 'a = UnsafeAny> {
data: hash_map::OccupiedEntry<'a, TypeId, Box<A>>,
_marker: PhantomData<K>
}
pub struct VacantEntry<'a, K, A: ?Sized + UnsafeAnyExt + 'a = UnsafeAny> {
data: hash_map::VacantEntry<'a, TypeId, Box<A>>,
_marker: PhantomData<K>
}
impl<'a, K: Key, A: UnsafeAnyExt + ?Sized> OccupiedEntry<'a, K, A> {
pub fn get(&self) -> &K::Value
where K::Value: Any + Implements<A> {
unsafe {
self.data.get().downcast_ref_unchecked()
}
}
pub fn get_mut(&mut self) -> &mut K::Value
where K::Value: Any + Implements<A> {
unsafe {
self.data.get_mut().downcast_mut_unchecked()
}
}
pub fn into_mut(self) -> &'a mut K::Value
where K::Value: Any + Implements<A> {
unsafe {
self.data.into_mut().downcast_mut_unchecked()
}
}
pub fn insert(&mut self, value: K::Value) -> K::Value
where K::Value: Any + Implements<A> {
unsafe {
*self.data.insert(value.into_object()).downcast_unchecked()
}
}
pub fn remove(self) -> K::Value
where K::Value: Any + Implements<A> {
unsafe {
*self.data.remove().downcast_unchecked()
}
}
}
impl<'a, K: Key, A: ?Sized + UnsafeAnyExt> VacantEntry<'a, K, A> {
pub fn insert(self, value: K::Value) -> &'a mut K::Value
where K::Value: Any + Implements<A> {
unsafe {
self.data.insert(value.into_object()).downcast_mut_unchecked()
}
}
}
mod internals;
#[cfg(test)]
mod test {
use super::{TypeMap, CloneMap, DebugMap, SendMap, Key};
use super::Entry::{Occupied, Vacant};
#[derive(Debug, PartialEq)]
struct KeyType;
#[derive(Clone, Debug, PartialEq)]
struct Value(u8);
impl Key for KeyType { type Value = Value; }
#[test] fn test_pairing() {
let mut map = TypeMap::new();
map.insert::<KeyType>(Value(100));
assert_eq!(*map.get::<KeyType>().unwrap(), Value(100));
assert!(map.contains::<KeyType>());
}
#[test] fn test_remove() {
let mut map = TypeMap::new();
map.insert::<KeyType>(Value(10));
assert!(map.contains::<KeyType>());
map.remove::<KeyType>();
assert!(!map.contains::<KeyType>());
}
#[test] fn test_entry() {
let mut map = TypeMap::new();
map.insert::<KeyType>(Value(20));
match map.entry::<KeyType>() {
Occupied(e) => {
assert_eq!(e.get(), &Value(20));
assert_eq!(e.remove(), Value(20));
},
_ => panic!("Unable to locate inserted item.")
}
assert!(!map.contains::<KeyType>());
match map.entry::<KeyType>() {
Vacant(e) => {
e.insert(Value(2));
},
_ => panic!("Found non-existant entry.")
}
assert!(map.contains::<KeyType>());
}
#[test] fn test_entry_or_insert() {
let mut map = TypeMap::new();
map.entry::<KeyType>().or_insert(Value(20)).0 += 1;
assert_eq!(map.get::<KeyType>().unwrap(), &Value(21));
map.entry::<KeyType>().or_insert(Value(100)).0 += 1;
assert_eq!(map.get::<KeyType>().unwrap(), &Value(22));
}
#[test] fn test_custom_bounds() {
let mut map: SendMap = TypeMap::custom();
map.insert::<KeyType>(Value(10));
assert!(map.contains::<KeyType>());
map.remove::<KeyType>();
assert!(!map.contains::<KeyType>());
}
#[test] fn test_clonemap() {
let mut map: CloneMap = TypeMap::custom();
map.insert::<KeyType>(Value(10));
assert!(map.contains::<KeyType>());
let cloned = map.clone();
assert_eq!(map.get::<KeyType>(), cloned.get::<KeyType>());
}
#[test] fn test_debugmap() {
let mut map: DebugMap = TypeMap::custom();
map.insert::<KeyType>(Value(10));
assert!(map.contains::<KeyType>());
}
}