1 macro_rules! if_downcast_into { 2 ($in_ty:ty, $out_ty:ty, $val:ident, $body:expr) => ({ 3 if std::any::TypeId::of::<$in_ty>() == std::any::TypeId::of::<$out_ty>() { 4 // Store the value in an `Option` so we can `take` 5 // it after casting to `&mut dyn Any`. 6 let mut slot = Some($val); 7 // Re-write the `$val` ident with the downcasted value. 8 let $val = (&mut slot as &mut dyn std::any::Any) 9 .downcast_mut::<Option<$out_ty>>() 10 .unwrap() 11 .take() 12 .unwrap(); 13 // Run the $body in scope of the replaced val. 14 $body 15 } 16 }) 17 } 18