Skip to content

Commit a7358ab

Browse files
committed
Add Bounds<...> trait to check characteristics of GodotClass types
1 parent 189bdee commit a7358ab

File tree

11 files changed

+424
-351
lines changed

11 files changed

+424
-351
lines changed

godot-codegen/src/class_generator.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,12 +604,18 @@ fn make_class(class: &Class, class_name: &TyName, ctx: &mut Context) -> Generate
604604
}
605605
};
606606

607-
let memory = if class_name.rust_ty == "Object" {
608-
ident("DynamicRefCount")
607+
let assoc_dyn_memory = if class_name.rust_ty == "Object" {
608+
ident("MemDynamic")
609609
} else if class.is_refcounted {
610-
ident("StaticRefCount")
610+
ident("MemRefCounted")
611611
} else {
612-
ident("ManualMemory")
612+
ident("MemManual")
613+
};
614+
615+
let assoc_memory = if class.is_refcounted {
616+
ident("MemRefCounted")
617+
} else {
618+
ident("MemManual")
613619
};
614620

615621
// mod re_export needed, because class should not appear inside the file module, and we can't re-export private struct as pub.
@@ -643,16 +649,19 @@ fn make_class(class: &Class, class_name: &TyName, ctx: &mut Context) -> Generate
643649
#internal_methods
644650
#constants
645651
}
646-
unsafe impl crate::obj::GodotClass for #class_name {
652+
impl crate::obj::GodotClass for #class_name {
647653
type Base = #base_ty;
648-
type Declarer = crate::obj::dom::EngineDomain;
649-
type Mem = crate::obj::mem::#memory;
650654
const INIT_LEVEL: Option<crate::init::InitLevel> = #init_level;
651655

652656
fn class_name() -> ClassName {
653657
ClassName::from_ascii_cstr(#class_name_cstr)
654658
}
655659
}
660+
unsafe impl crate::obj::Bounds for #class_name {
661+
type Memory = crate::obj::bounds::#assoc_memory;
662+
type DynMemory = crate::obj::bounds::#assoc_dyn_memory;
663+
type Declarer = crate::obj::bounds::DeclEngine;
664+
}
656665
impl crate::obj::EngineClass for #class_name {
657666
fn as_object_ptr(&self) -> sys::GDExtensionObjectPtr {
658667
self.object_ptr

godot-core/src/builder/method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ macro_rules! impl_code_method {
117117
118118
$(
119119
let $arg = <$Param as sys::GodotFfi>::from_sys(*args.offset(idx));
120-
// FIXME update refcount, e.g. Gd::ready() or T::Mem::maybe_inc_ref(&result);
120+
// FIXME update refcount, e.g. Gd::ready() or T::DynMemory::maybe_inc_ref(&result);
121121
// possibly in from_sys() directly; what about from_sys_init() and from_{obj|str}_sys()?
122122
idx += 1;
123123
)*

godot-core/src/builtin/callable.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use godot_ffi as sys;
1010
use crate::builtin::meta::{impl_godot_as_self, GodotType, ToGodot};
1111
use crate::builtin::{inner, StringName, Variant, VariantArray};
1212
use crate::engine::Object;
13-
use crate::obj::mem::Memory;
13+
use crate::obj::bounds::DynMemory;
14+
use crate::obj::Bounds;
1415
use crate::obj::{Gd, GodotClass, InstanceId};
1516
use std::{fmt, ptr};
1617
use sys::{ffi_methods, GodotFfi};
@@ -202,7 +203,7 @@ impl Callable {
202203
// Increment refcount because we're getting a reference, and `InnerCallable::get_object` doesn't
203204
// increment the refcount.
204205
self.as_inner().get_object().map(|object| {
205-
<Object as GodotClass>::Mem::maybe_inc_ref(&object.raw);
206+
<Object as Bounds>::DynMemory::maybe_inc_ref(&object.raw);
206207
object
207208
})
208209
}

godot-core/src/engine/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
//! Godot engine classes and methods.
99
1010
use crate::builtin::{GString, NodePath};
11-
use crate::obj::dom::EngineDomain;
12-
use crate::obj::{Gd, GodotClass, Inherits, InstanceId};
11+
use crate::obj::{bounds, Bounds, Gd, GodotClass, Inherits, InstanceId};
1312
use std::collections::HashSet;
1413

1514
// Re-exports of generated symbols
@@ -115,7 +114,7 @@ impl NodeExt for Node {
115114

116115
impl<U> NodeExt for Gd<U>
117116
where
118-
U: GodotClass<Declarer = EngineDomain> + Inherits<Node>,
117+
U: Bounds<Declarer = bounds::DeclEngine> + Inherits<Node>,
119118
{
120119
fn try_get_node_as<T>(&self, path: impl Into<NodePath>) -> Option<Gd<T>>
121120
where
@@ -163,7 +162,7 @@ pub(crate) fn object_ptr_from_id(instance_id: InstanceId) -> sys::GDExtensionObj
163162

164163
pub(crate) fn construct_engine_object<T>() -> Gd<T>
165164
where
166-
T: GodotClass<Declarer = EngineDomain>,
165+
T: GodotClass + Bounds<Declarer = bounds::DeclEngine>,
167166
{
168167
// SAFETY: adhere to Godot API; valid class name and returned pointer is an object.
169168
unsafe {

0 commit comments

Comments
 (0)