Closed
Description
If you implement GodotClass
for T
with Declarer = EngineDomain
, then you can use Deref
to transmute a &OpaqueObject
to a &T
. It is the responsibility of the implementer of GodotClass
to ensure that this is a safe transmute.
See:
impl<T> Deref for Gd<T>
where
T: GodotClass<Declarer = dom::EngineDomain>,
{
type Target = T;
fn deref(&self) -> &Self::Target {
// SAFETY:
// This relies on Gd<Node3D> having the layout as Node3D (as an example),
// which also needs #[repr(transparent)]:
//
// struct Gd<T: GodotClass> {
// opaque: OpaqueObject, <- size of GDExtensionObjectPtr
// _marker: PhantomData, <- ZST
// }
// struct Node3D {
// object_ptr: sys::GDExtensionObjectPtr,
// }
unsafe { std::mem::transmute::<&OpaqueObject, &T>(&self.opaque) }
}
}