18 lines
469 B
Rust
18 lines
469 B
Rust
use std::{any::TypeId, mem::ManuallyDrop};
|
|
|
|
/// Safe cast using [`TypeId`].
|
|
#[inline]
|
|
pub fn cast<T: 'static, R: 'static>(t: T) -> Result<R, T> {
|
|
if TypeId::of::<T>() == TypeId::of::<R>() {
|
|
union U<T, R> {
|
|
t: ManuallyDrop<T>,
|
|
r: ManuallyDrop<R>,
|
|
}
|
|
|
|
// SAFETY: we've confirmed the types are the same.
|
|
Ok(unsafe { ManuallyDrop::into_inner(U { t: ManuallyDrop::new(t) }.r) })
|
|
} else {
|
|
Err(t)
|
|
}
|
|
}
|