diff --git a/src/if_type.rs b/src/if_type.rs index 6d13314..8b8c54c 100644 --- a/src/if_type.rs +++ b/src/if_type.rs @@ -1,13 +1,14 @@ use std::any::TypeId; +/// run if types are identical #[inline] pub fn if_type(f: impl FnOnce() -> Specific) -> Option { if TypeId::of::() == TypeId::of::() { let typed = f(); - let erased: T = unsafe{ std::mem::transmute_copy(&typed) }; - std::mem::forget(typed); + let erased: T = unsafe{ std::mem::transmute_copy(&typed) }; //type and alignment ensured by identical type + std::mem::forget(typed); //else we get some fancy double free Some(erased) }else{ None } -} \ No newline at end of file +}