protobuf/reflect/repeated/
transmute.rs

1use std::any::TypeId;
2
3pub(crate) fn transmute_mut_if_eq<A: 'static, B: 'static>(a: &mut A) -> Result<&mut B, &mut A> {
4    if TypeId::of::<A>() == TypeId::of::<B>() {
5        // SAFETY: we check type before transmuting.
6        Ok(unsafe { &mut *(a as *mut A as *mut B) })
7    } else {
8        Err(a)
9    }
10}
11
12pub(crate) fn transmute_ref_if_eq<A: 'static, B: 'static>(a: &A) -> Result<&B, &A> {
13    if TypeId::of::<A>() == TypeId::of::<B>() {
14        // SAFETY: we check type before transmuting.
15        Ok(unsafe { &*(a as *const A as *const B) })
16    } else {
17        Err(a)
18    }
19}