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
use std::any::{Any, TypeId};
use std::cell::UnsafeCell;
use std::collections::HashMap;
use std::fmt;
use std::mem;
use std::ops::Deref;
pub struct OptCell<T>(UnsafeCell<Option<T>>);
impl<T> OptCell<T> {
#[inline]
pub fn new(val: Option<T>) -> OptCell<T> {
OptCell(UnsafeCell::new(val))
}
#[inline]
pub fn set(&self, val: T) {
unsafe {
let opt = self.0.get();
debug_assert!((*opt).is_none());
*opt = Some(val)
}
}
#[inline]
pub unsafe fn get_mut(&mut self) -> &mut T {
let opt = &mut *self.0.get();
opt.as_mut().unwrap()
}
}
impl<T> Deref for OptCell<T> {
type Target = Option<T>;
#[inline]
fn deref(&self) -> &Option<T> {
unsafe { &*self.0.get() }
}
}
impl<T: Clone> Clone for OptCell<T> {
#[inline]
fn clone(&self) -> OptCell<T> {
OptCell::new((**self).clone())
}
}
pub struct PtrMapCell<V: ?Sized>(UnsafeCell<PtrMap<Box<V>>>);
#[derive(Clone, Debug)]
enum PtrMap<T> {
Empty,
One(TypeId, T),
Many(HashMap<TypeId, T>)
}
impl<V: ?Sized + fmt::Debug + Any + 'static> PtrMapCell<V> {
#[inline]
pub fn new() -> PtrMapCell<V> {
PtrMapCell(UnsafeCell::new(PtrMap::Empty))
}
#[inline]
pub fn get(&self, key: TypeId) -> Option<&V> {
let map = unsafe { &*self.0.get() };
match *map {
PtrMap::Empty => None,
PtrMap::One(id, ref v) => if id == key {
Some(v)
} else {
None
},
PtrMap::Many(ref hm) => hm.get(&key)
}.map(|val| &**val)
}
#[inline]
pub fn get_mut(&mut self, key: TypeId) -> Option<&mut V> {
let map = unsafe { &mut *self.0.get() };
match *map {
PtrMap::Empty => None,
PtrMap::One(id, ref mut v) => if id == key {
Some(v)
} else {
None
},
PtrMap::Many(ref mut hm) => hm.get_mut(&key)
}.map(|val| &mut **val)
}
#[inline]
pub unsafe fn insert(&self, key: TypeId, val: Box<V>) {
let map = &mut *self.0.get();
match *map {
PtrMap::Empty => *map = PtrMap::One(key, val),
PtrMap::One(..) => {
let one = mem::replace(map, PtrMap::Empty);
match one {
PtrMap::One(id, one) => {
debug_assert!(id != key);
let mut hm = HashMap::with_capacity(2);
hm.insert(id, one);
hm.insert(key, val);
mem::replace(map, PtrMap::Many(hm));
},
_ => unreachable!()
}
},
PtrMap::Many(ref mut hm) => { hm.insert(key, val); }
}
}
#[inline]
pub unsafe fn one(&self) -> &V {
let map = &*self.0.get();
match *map {
PtrMap::One(_, ref one) => one,
_ => panic!("not PtrMap::One value, {:?}", *map)
}
}
}
impl<V: ?Sized + fmt::Debug + Any + 'static> Clone for PtrMapCell<V> where Box<V>: Clone {
#[inline]
fn clone(&self) -> PtrMapCell<V> {
let cell = PtrMapCell::new();
unsafe {
*cell.0.get() = (&*self.0.get()).clone()
}
cell
}
}
#[cfg(test)]
mod test {
use std::any::TypeId;
use super::*;
#[test]
fn test_opt_cell_set() {
let one:OptCell<u32> = OptCell::new(None);
one.set(1);
assert_eq!(*one,Some(1));
}
#[test]
fn test_opt_cell_clone() {
let one:OptCell<u32> = OptCell::new(Some(3));
let stored = *one.clone();
assert_eq!(stored,Some(3));
}
#[test]
fn test_ptr_map_cell_none() {
let type_id = TypeId::of::<u32>();
let pm:PtrMapCell<u32> = PtrMapCell::new();
assert_eq!(pm.get(type_id),None);
}
#[test]
fn test_ptr_map_cell_one() {
let type_id = TypeId::of::<String>();
let pm:PtrMapCell<String> = PtrMapCell::new();
unsafe { pm.insert(type_id, Box::new("a".to_string())); }
assert_eq!(pm.get(type_id), Some(&"a".to_string()));
assert_eq!(unsafe {pm.one()}, "a");
}
#[test]
fn test_ptr_map_cell_two() {
let type_id = TypeId::of::<String>();
let type_id2 = TypeId::of::<Vec<u8>>();
let pm:PtrMapCell<String> = PtrMapCell::new();
unsafe { pm.insert(type_id, Box::new("a".to_string())); }
unsafe { pm.insert(type_id2, Box::new("b".to_string())); }
assert_eq!(pm.get(type_id), Some(&"a".to_string()));
assert_eq!(pm.get(type_id2), Some(&"b".to_string()));
}
#[test]
fn test_ptr_map_cell_many() {
let id1 = TypeId::of::<String>();
let id2 = TypeId::of::<Vec<u8>>();
let id3 = TypeId::of::<OptCell<String>>();
let pm:PtrMapCell<String> = PtrMapCell::new();
unsafe { pm.insert(id1, Box::new("a".to_string())); }
unsafe { pm.insert(id2, Box::new("b".to_string())); }
unsafe { pm.insert(id3, Box::new("c".to_string())); }
assert_eq!(pm.get(id1), Some(&"a".to_string()));
assert_eq!(pm.get(id2), Some(&"b".to_string()));
assert_eq!(pm.get(id3), Some(&"c".to_string()));
}
#[test]
fn test_ptr_map_cell_clone() {
let type_id = TypeId::of::<String>();
let pm:PtrMapCell<String> = PtrMapCell::new();
unsafe { pm.insert(type_id, Box::new("a".to_string())); }
let cloned = pm.clone();
assert_eq!(cloned.get(type_id), Some(&"a".to_string()));
}
}