2022-02-06 01:29:05 +01:00
|
|
|
/// Runs `$code` if `$old` is of type `$t`.
|
2022-02-06 16:02:59 +01:00
|
|
|
///
|
|
|
|
/// This macro is primarily used for type casting between known types.
|
2022-02-06 01:29:05 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! reify {
|
|
|
|
($old:ident, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
|
|
|
|
#[allow(unused_imports)]
|
2022-02-06 16:02:59 +01:00
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
if std::any::TypeId::of::<$t>() == $old.type_id() {
|
2022-02-06 01:29:05 +01:00
|
|
|
// SAFETY: This is safe because we check to make sure the two types are
|
|
|
|
// actually the same type.
|
2022-02-06 16:02:59 +01:00
|
|
|
let $new: $t = unsafe { std::mem::transmute_copy(&std::mem::ManuallyDrop::new($old)) };
|
2022-02-06 01:29:05 +01:00
|
|
|
$code
|
|
|
|
} else {
|
|
|
|
$fallback
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
($old:expr, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
|
|
|
|
let old = $old;
|
2022-02-06 16:02:59 +01:00
|
|
|
reify!(old, |$new: $t| $code, || $fallback)
|
2022-02-06 01:29:05 +01:00
|
|
|
}};
|
|
|
|
($old:ident, |$new:ident : $t:ty| $code:expr) => {
|
2022-02-06 16:02:59 +01:00
|
|
|
reify!($old, |$new: $t| $code, || ())
|
2022-02-06 01:29:05 +01:00
|
|
|
};
|
|
|
|
($old:expr, |$new:ident : $t:ty| $code:expr) => {
|
2022-02-06 16:02:59 +01:00
|
|
|
reify!($old, |$new: $t| $code, || ())
|
2022-02-06 01:29:05 +01:00
|
|
|
};
|
|
|
|
}
|
2022-02-06 16:02:59 +01:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! reify_dynamic {
|
|
|
|
($old:ident, |$new:ident : $t:ty| $code:expr) => {{
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use ::std::{
|
|
|
|
any::{Any, TypeId},
|
|
|
|
mem::{transmute_copy, ManuallyDrop},
|
|
|
|
};
|
|
|
|
if TypeId::of::<$t>() == TypeId::of::<crate::Dynamic>() {
|
|
|
|
// SAFETY: This is safe because we check to make sure the two types are
|
|
|
|
// actually the same type.
|
|
|
|
let $new: $t = unsafe { transmute_copy(&ManuallyDrop::new($old)) };
|
|
|
|
$code
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|