Skip to content

Commit

Permalink
Make it clear the class_name_or_default is only used for diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
chitoyuu committed Jan 9, 2022
1 parent 5ed9f10 commit fecf465
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
25 changes: 13 additions & 12 deletions gdnative-core/src/export/class_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ pub(crate) struct ClassInfo {
pub name: Cow<'static, str>,
}

/// Can be used to validate whether or not `C` has been added using `InitHandle::add_class<C>()`
/// Returns true if added otherwise false.
#[inline]
pub(crate) fn is_class_registered<C: NativeClass>() -> bool {
let type_id = TypeId::of::<C>();
CLASS_REGISTRY.read().contains_key(&type_id)
}

/// Access the [`ClassInfo`] of the class `C`.
#[inline]
pub(crate) fn with_class_info<C: NativeClass, F, R>(f: F) -> Option<R>
Expand All @@ -31,12 +23,21 @@ where
CLASS_REGISTRY.read().get(&TypeId::of::<C>()).map(f)
}

/// Returns the NativeScript name of the class `C` if it is registered. Returns a best-effort
/// description of the type for error reporting otherwise.
/// Returns the NativeScript name of the class `C` if it is registered.
/// Can also be used to validate whether or not `C` has been added using `InitHandle::add_class<C>()`
#[inline]
pub(crate) fn class_name_or_default<C: NativeClass>() -> Cow<'static, str> {
pub(crate) fn class_name<C: NativeClass>() -> Option<Cow<'static, str>> {
with_class_info::<C, _, _>(|i| i.name.clone())
.unwrap_or_else(|| Cow::Borrowed(std::any::type_name::<C>()))
}

/// Returns the NativeScript name of the class `C` if it is registered, or a best-effort description
/// of the type otherwise.
///
/// The returned string should only be used for diagnostic purposes, not for types that the user
/// has to name explicitly. The format is not guaranteed.
#[inline]
pub(crate) fn class_name_or_default<C: NativeClass>() -> Cow<'static, str> {
class_name::<C>().unwrap_or_else(|| Cow::Borrowed(std::any::type_name::<C>()))
}

/// Registers the class `C` in the class registry, using a custom name.
Expand Down
12 changes: 9 additions & 3 deletions gdnative-core/src/object/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ impl<T: NativeClass> Instance<T, Unique> {
std::ptr::null_mut(),
);

let script_class_name = GodotString::from(class_registry::class_name_or_default::<T>());
let script_class_name = class_registry::class_name::<T>()
.map(GodotString::from)
.unwrap_or_else(|| {
panic!(
"`{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>(),
);
});

let mut args: [*const libc::c_void; 1] = [script_class_name.sys() as *const _];
(gd_api.godot_method_bind_ptrcall)(
nativescript_methods.set_class_name,
Expand All @@ -170,8 +178,6 @@ impl<T: NativeClass> Instance<T, Unique> {
std::ptr::null_mut(),
);

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>());

assert!(
emplace::take::<T>().is_none(),
"emplacement value should be taken by the constructor wrapper (this is a bug in the bindings)",
Expand Down

0 comments on commit fecf465

Please sign in to comment.