1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102

unsafe impl GodotObject for Object {
    fn class_name() -> &'static str {
        "Object"
    }

    unsafe fn from_sys(obj: *mut sys::godot_object) -> Self {
        // Not reference-counted.
        Self { this: obj, }
    }

    unsafe fn from_return_position_sys(obj: *mut sys::godot_object) -> Self {
        Self { this: obj, }
    }

    unsafe fn to_sys(&self) -> *mut sys::godot_object {
        self.this
    }
}

impl ToVariant for Object {
    fn to_variant(&self) -> Variant { Variant::from_object(self) }
}
impl FromVariant for Object {
    fn from_variant(variant: &Variant) -> Result<Self, FromVariantError> { variant.try_to_object_with_error::<Self>() }
}

impl Free for Object {
    unsafe fn godot_free(self) { self.free() }
}

impl Instanciable for Object {
    fn construct() -> Self {
        Object::new()
    }
}

unsafe impl GodotObject for Reference {
    fn class_name() -> &'static str {
        "Reference"
    }

    unsafe fn from_sys(obj: *mut sys::godot_object) -> Self {
        object::add_ref(obj);
        Self { this: obj, }
    }

    unsafe fn from_return_position_sys(obj: *mut sys::godot_object) -> Self {
        Self { this: obj, }
    }

    unsafe fn to_sys(&self) -> *mut sys::godot_object {
        self.this
    }
}

impl ToVariant for Reference {
    fn to_variant(&self) -> Variant { Variant::from_object(self) }
}
impl FromVariant for Reference {
    fn from_variant(variant: &Variant) -> Result<Self, FromVariantError> { variant.try_to_object_with_error::<Self>() }
}

impl std::ops::Deref for Reference {
    type Target = Object;

    fn deref(&self) -> &Object {
        unsafe {
            std::mem::transmute(self)
        }
    }
}

impl std::ops::DerefMut for Reference {
    fn deref_mut(&mut self) -> &mut Object {
        unsafe {
            std::mem::transmute(self)
        }
    }
}

impl Clone for Reference {
    fn clone(&self) -> Self {
        self.new_ref()
    }
}

impl Drop for Reference {
    fn drop(&mut self) {
        unsafe {
            if object::unref(self.this) {
                (get_api().godot_object_destroy)(self.this);
            }
        }
    }
}

impl Instanciable for Reference {
    fn construct() -> Self {
        Reference::new()
    }
}