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
#[macro_export]
macro_rules! reify {
($old:ident, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
if std::any::TypeId::of::<$t>() == std::any::Any::type_id(&$old) {
let $new: $t = unsafe { std::mem::transmute_copy(&std::mem::ManuallyDrop::new($old)) };
$code
} else {
$fallback
}
}};
($old:expr, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
let old = $old;
reify!(old, |$new: $t| $code, || $fallback)
}};
($old:ident, |$new:ident : $t:ty| $code:expr) => {
reify!($old, |$new: $t| $code, || ())
};
($old:expr, |$new:ident : $t:ty| $code:expr) => {
reify!($old, |$new: $t| $code, || ())
};
($old:ident => Option<$t:ty>) => {
reify!($old, |v: $t| Some(v), || None)
};
($old:expr => Option<$t:ty>) => {
reify!($old, |v: $t| Some(v), || None)
};
($old:ident => $t:ty) => {
reify!($old, |v: $t| v, || unreachable!())
};
($old:expr => $t:ty) => {
reify!($old, |v: $t| v, || unreachable!())
};
}