Skip to content

Commit fecf465

Browse files
committed
Make it clear the class_name_or_default is only used for diagnostics
1 parent 5ed9f10 commit fecf465

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

gdnative-core/src/export/class_registry.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ pub(crate) struct ClassInfo {
1414
pub name: Cow<'static, str>,
1515
}
1616

17-
/// Can be used to validate whether or not `C` has been added using `InitHandle::add_class<C>()`
18-
/// Returns true if added otherwise false.
19-
#[inline]
20-
pub(crate) fn is_class_registered<C: NativeClass>() -> bool {
21-
let type_id = TypeId::of::<C>();
22-
CLASS_REGISTRY.read().contains_key(&type_id)
23-
}
24-
2517
/// Access the [`ClassInfo`] of the class `C`.
2618
#[inline]
2719
pub(crate) fn with_class_info<C: NativeClass, F, R>(f: F) -> Option<R>
@@ -31,12 +23,21 @@ where
3123
CLASS_REGISTRY.read().get(&TypeId::of::<C>()).map(f)
3224
}
3325

34-
/// Returns the NativeScript name of the class `C` if it is registered. Returns a best-effort
35-
/// description of the type for error reporting otherwise.
26+
/// Returns the NativeScript name of the class `C` if it is registered.
27+
/// Can also be used to validate whether or not `C` has been added using `InitHandle::add_class<C>()`
3628
#[inline]
37-
pub(crate) fn class_name_or_default<C: NativeClass>() -> Cow<'static, str> {
29+
pub(crate) fn class_name<C: NativeClass>() -> Option<Cow<'static, str>> {
3830
with_class_info::<C, _, _>(|i| i.name.clone())
39-
.unwrap_or_else(|| Cow::Borrowed(std::any::type_name::<C>()))
31+
}
32+
33+
/// Returns the NativeScript name of the class `C` if it is registered, or a best-effort description
34+
/// of the type otherwise.
35+
///
36+
/// The returned string should only be used for diagnostic purposes, not for types that the user
37+
/// has to name explicitly. The format is not guaranteed.
38+
#[inline]
39+
pub(crate) fn class_name_or_default<C: NativeClass>() -> Cow<'static, str> {
40+
class_name::<C>().unwrap_or_else(|| Cow::Borrowed(std::any::type_name::<C>()))
4041
}
4142

4243
/// Registers the class `C` in the class registry, using a custom name.

gdnative-core/src/object/instance.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,15 @@ impl<T: NativeClass> Instance<T, Unique> {
148148
std::ptr::null_mut(),
149149
);
150150

151-
let script_class_name = GodotString::from(class_registry::class_name_or_default::<T>());
151+
let script_class_name = class_registry::class_name::<T>()
152+
.map(GodotString::from)
153+
.unwrap_or_else(|| {
154+
panic!(
155+
"`{type_name}` must be registered before it can be used; call `handle.add_class::<{type_name}>()` in your `nativescript_init` callback",
156+
type_name = std::any::type_name::<T>(),
157+
);
158+
});
159+
152160
let mut args: [*const libc::c_void; 1] = [script_class_name.sys() as *const _];
153161
(gd_api.godot_method_bind_ptrcall)(
154162
nativescript_methods.set_class_name,
@@ -170,8 +178,6 @@ impl<T: NativeClass> Instance<T, Unique> {
170178
std::ptr::null_mut(),
171179
);
172180

173-
debug_assert!(class_registry::is_class_registered::<T>(), "`{type_name}` must be registered before it can be used; call `handle.add_class::<{type_name}>()` in your `nativescript_init` callback", type_name = std::any::type_name::<T>());
174-
175181
assert!(
176182
emplace::take::<T>().is_none(),
177183
"emplacement value should be taken by the constructor wrapper (this is a bug in the bindings)",

0 commit comments

Comments
 (0)