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
use crateFunctionLabel;
use crateTaskItem;
use crateSquashTypeHelper;
use crateHelperMap;
use crateHelperByType;
use crateRemoteSend;
use crateset_helpers;
use BoxFuture;
use Lazy;
use FxHashMap;
use Cell;
use Mutex;
use TypeId;
extern crate futures;
pub extern crate inventory;
pub type DispatchFunction = fn ;
collect!;
type FuncMetaTable = ;
static STATIC_FUNC_META_TABLE: =
new;
pub
pub
collect!;
pub
// This mod is trying to do something like "check types at compiling time"
// Dark Magic: Just register all type of squashable, and manually create dyn trait at runtime
// If rust support specialization someday, will remove this.
//
// #[repr(C)]
// #[derive(Debug, Clone)]
// struct TypeMetaInfo {
// type_id: TypeId, // type id of squashable
// vtable: *const (), // vtable of suqashble as squashobject
// }
//
// unsafe impl Send for TypeMetaInfo {}
//
// #[repr(C)]
// #[derive(Debug)]
// pub struct TraitObject {
// pub data: *mut (),
// pub vtable: *const (),
// }
//
// #[repr(C)]
// union PtrRepr<T: ?Sized> {
// const_ptr: *const T,
// mut_ptr: *mut T,
// trait_obj: TraitObject,
// }
//
// impl Copy for TraitObject {}
//
// // Manual impl needed to avoid `T: Clone` bound.
// impl Clone for TraitObject {
// fn clone(&self) -> Self {
// *self
// }
// }
//
// // type table for squashable types
// type TypeMetaInfoTable = FxHashMap<TypeId, TypeMetaInfo>;
// static STATIC_META_TABLE: Lazy<Mutex<TypeMetaInfoTable>> =
// Lazy::new(|| Mutex::new(TypeMetaInfoTable::default()));
//
// fn get_meta_table() -> &'static TypeMetaInfoTable {
// thread_local! {
// static META_TABLE: Cell<Option<TypeMetaInfoTable>> = Cell::new(None);
// }
// META_TABLE.with(|s| {
// let maybe_ref: &Option<_> = unsafe { &*s.as_ptr() };
// if let Some(s_ref) = maybe_ref.as_ref() {
// s_ref
// } else {
// let cloned = STATIC_META_TABLE.lock().unwrap().clone();
// s.set(Some(cloned));
// get_meta_table()
// }
// })
// }
//
// impl TypeMetaInfo {
// fn new<T>() -> Self
// where
// T: RemoteSend + Any + 'static,
// {
// let fake_value = mem::MaybeUninit::<T>::uninit();
// let fake_value = unsafe { fake_value.assume_init() };
// let fake_ref = &fake_value as &dyn SquashableObject;
// TypeMetaInfo {
// type_id: TypeId::of::<T>(),
// vtable: Self::trait_object::<dyn SquashableObject>(fake_ref).vtable,
// }
// }
// fn trait_object<T: ?Sized>(ptr: &T) -> TraitObject {
// unsafe {
// PtrRepr {
// const_ptr: ptr as *const T,
// }
// .trait_obj
// }
// }
// }
//
// pub fn register_squashable<T>()
// where
// T: RemoteSend + Any + 'static,
// {
// let meta_info = TypeMetaInfo::new::<T>();
// STATIC_META_TABLE
// .lock()
// .unwrap()
// .insert(TypeId::of::<T>(), meta_info);
// }
//
// pub fn try_cast_squashable_object<T>(t: T) -> Result<Box<dyn SquashableObject>, T>
// where
// T: Any + 'static,
// {
// if let Some(meta_info) = get_meta_table().get(&TypeId::of::<T>()) {
// unsafe {
// let vtable = meta_info.vtable;
// let b = Box::new(t);
// let data = Box::into_raw(b) as *mut ();
// let trait_ptr = PtrRepr::<dyn SquashableObject> {
// trait_obj: TraitObject { data, vtable },
// }
// .mut_ptr;
// Ok(Box::from_raw(trait_ptr))
// }
// } else {
// Err(t)
// }
// }
//
// #[cfg(test)]
// mod test {
// use super::*;
// use crate::activity::test::A;
// use crate::activity::test::B;
//
// // WARN: only one test case will touch the static data
// fn clean_and_set_meta_info() {
// *STATIC_META_TABLE.lock().unwrap() = TypeMetaInfoTable::default();
// register_squashable::<A>();
// register_squashable::<B>();
// }
//
// use crate::activity::downcast_squashable;
//
// fn test_cast_of<T: RemoteSend + std::fmt::Debug + Clone>(list: Vec<Box<T>>) {
// let mut a_obj_list = vec![];
// for a in list.iter().cloned() {
// let result = try_cast_squashable_object(*a);
// assert!(result.is_ok());
// a_obj_list.push(result.unwrap());
// }
//
// // squash
// let mut default_aout = a_obj_list[0].default_squashed();
// for a in a_obj_list.iter() {
// a.fold(&mut default_aout)
// }
// // inflate
// let mut inflated = vec![];
// while let Some(a) = default_aout.extract() {
// inflated.push(a)
// }
// let inflated: Vec<_> = inflated
// .into_iter()
// .rev()
// .map(|a| downcast_squashable::<T>(a).unwrap())
// .collect();
// assert_eq!(inflated, list);
// }
//
// #[test]
// pub fn test_cast() {
// clean_and_set_meta_info();
//
// let num1 = 1usize;
// assert_eq!(try_cast_squashable_object(num1).err().unwrap(), num1);
//
// let a_list: Vec<_> = (0..128).map(|i| Box::new(A { value: i })).collect();
// test_cast_of(a_list);
//
// let b_list: Vec<_> = (0..128).map(|i| Box::new(B { value: i })).collect();
// test_cast_of(b_list);
// }
// }