Skip to content

GodotClass should be an unsafe trait #345

Closed
@lilizoey

Description

@lilizoey

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) }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugc: registerRegister classes, functions and other symbols to GDScript

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions