Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
doc
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertz19281 committed Jan 26, 2021
1 parent 0107a92 commit f4eae5b
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/if_type.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::any::TypeId;

/// run if types are identical
#[inline]
pub fn if_type<Specific: 'static, T: 'static>(f: impl FnOnce() -> Specific) -> Option<T> {
if TypeId::of::<T>() == TypeId::of::<Specific>() {
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
}
}
}

0 comments on commit f4eae5b

Please sign in to comment.