rhai/src/reify.rs

41 lines
1.3 KiB
Rust
Raw Normal View History

/// 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.
#[macro_export]
macro_rules! reify {
($old:ident, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
2022-02-07 05:02:00 +01:00
if std::any::TypeId::of::<$t>() == std::any::Any::type_id(&$old) {
// 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)) };
$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)
}};
($old:ident, |$new:ident : $t:ty| $code:expr) => {
2022-02-06 16:02:59 +01:00
reify!($old, |$new: $t| $code, || ())
};
($old:expr, |$new:ident : $t:ty| $code:expr) => {
2022-02-06 16:02:59 +01:00
reify!($old, |$new: $t| $code, || ())
};
2022-02-08 02:25:53 +01:00
($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!())
};
}