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
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct Context {
type_map: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
}
impl Default for Context {
fn default() -> Self {
Self::new()
}
}
impl Context {
pub fn new() -> Self {
Self {
type_map: HashMap::new(),
}
}
pub fn insert_or_replace<E>(&mut self, entity: E) -> Option<Arc<E>>
where
E: Send + Sync + 'static,
{
self.type_map
.insert(TypeId::of::<E>(), Arc::new(entity))
.map(|displaced| displaced.downcast().expect("failed to unwrap downcast"))
}
pub fn insert<E>(&mut self, entity: E) -> &mut Self
where
E: Send + Sync + 'static,
{
self.type_map.insert(TypeId::of::<E>(), Arc::new(entity));
self
}
pub fn remove<E>(&mut self) -> Option<Arc<E>>
where
E: Send + Sync + 'static,
{
self.type_map
.remove(&TypeId::of::<E>())
.map(|removed| removed.downcast().expect("failed to unwrap downcast"))
}
pub fn get<E>(&self) -> Option<&E>
where
E: Send + Sync + 'static,
{
self.type_map
.get(&TypeId::of::<E>())
.and_then(|item| item.downcast_ref())
}
pub fn len(&self) -> usize {
self.type_map.len()
}
pub fn is_empty(&self) -> bool {
self.type_map.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
#[test]
fn insert_get_string() {
let mut context = Context::new();
context.insert_or_replace("pollo".to_string());
assert_eq!(Some(&"pollo".to_string()), context.get());
}
#[test]
fn insert_get_custom_structs() {
#[derive(Debug, PartialEq, Eq)]
struct S1 {}
#[derive(Debug, PartialEq, Eq)]
struct S2 {}
let mut context = Context::new();
context.insert_or_replace(S1 {});
context.insert_or_replace(S2 {});
assert_eq!(Some(Arc::new(S1 {})), context.insert_or_replace(S1 {}));
assert_eq!(Some(Arc::new(S2 {})), context.insert_or_replace(S2 {}));
assert_eq!(Some(&S1 {}), context.get());
assert_eq!(Some(&S2 {}), context.get());
}
#[test]
fn insert_fluent_syntax() {
#[derive(Debug, PartialEq, Eq, Default)]
struct S1 {}
#[derive(Debug, PartialEq, Eq, Default)]
struct S2 {}
let mut context = Context::new();
context
.insert("static str")
.insert("a String".to_string())
.insert(S1::default())
.insert(S1::default())
.insert(S2::default());
assert_eq!(4, context.len());
assert_eq!(Some(&"static str"), context.get());
}
fn require_send_sync<T: Send + Sync>(_: &T) {}
#[test]
fn test_require_send_sync() {
require_send_sync(&Context::new())
}
#[test]
fn mutability() {
#[derive(Debug, PartialEq, Eq, Default)]
struct S1 {
num: u8,
}
let mut context = Context::new();
context.insert_or_replace(Mutex::new(S1::default()));
assert_eq!(0, context.get::<Mutex<S1>>().unwrap().lock().unwrap().num);
context.get::<Mutex<S1>>().unwrap().lock().unwrap().num = 42;
assert_eq!(42, context.get::<Mutex<S1>>().unwrap().lock().unwrap().num);
let displaced = context
.insert_or_replace(Mutex::new(S1::default()))
.unwrap();
assert_eq!(42, displaced.lock().unwrap().num);
assert_eq!(0, context.get::<Mutex<S1>>().unwrap().lock().unwrap().num);
context.insert_or_replace(Mutex::new(33u32));
*context.get::<Mutex<u32>>().unwrap().lock().unwrap() = 42;
assert_eq!(42, *context.get::<Mutex<u32>>().unwrap().lock().unwrap());
}
}