Skip to content

Commit

Permalink
Merge pull request #593 from godot-rust/feature/no-init
Browse files Browse the repository at this point in the history
`#[class(no_init)]` to explicitly disable constructor
  • Loading branch information
Bromeon authored Feb 4, 2024
2 parents b4a91a6 + d0e99a4 commit cd652b7
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 160 deletions.
11 changes: 5 additions & 6 deletions godot-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,20 @@ pub mod private {
// https://github.com/rust-lang/rust/pull/58994. Fortunately, an extra layer of indirection solves most problems: we generate a declarative
// macro that itself isn't deprecated, but _its_ expansion is. Since the expansion happens in a later step, the warning is emitted.

#[doc(hidden)]
#[inline(always)]
#[deprecated = "#[base] is no longer needed; Base<T> is recognized directly. \n\
More information on https://github.com/godot-rust/gdext/pull/577."]
pub const fn base_attribute_warning() {}
pub const fn base_attribute() {}

#[doc(hidden)]
#[macro_export]
macro_rules! __base_attribute_warning_expand {
() => {
const _: () = $crate::private::base_attribute_warning();
macro_rules! __emit_deprecated_warning {
($warning_fn:ident) => {
const _: () = $crate::private::$warning_fn();
};
}

pub use crate::__base_attribute_warning_expand;
pub use crate::__emit_deprecated_warning;
}

macro_rules! generate_gdextension_api_version {
Expand Down
14 changes: 14 additions & 0 deletions godot-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub enum PluginItem {

/// Whether `#[class(hidden)]` was used.
is_hidden: bool,

/// Whether the class has a default constructor.
is_instantiable: bool,
},

/// Collected from `#[godot_api] impl MyClass`.
Expand Down Expand Up @@ -377,9 +380,20 @@ fn fill_class_info(item: PluginItem, c: &mut ClassRegistrationInfo) {
default_get_virtual_fn,
is_editor_plugin,
is_hidden,
is_instantiable,
} => {
c.parent_class_name = Some(base_class_name);

// Classes marked #[class(no_init)] are translated to "abstract" in Godot. This disables their default constructor.
// "Abstract" is a misnomer -- it's not an abstract base class, but rather a "utility/static class" (although it can have instance
// methods). Examples are Input, IP, FileAccess, DisplayServer.
//
// Abstract base classes on the other hand are called "virtual" in Godot. Examples are Mesh, Material, Texture.
// For some reason, certain ABCs like PhysicsBody2D are not marked "virtual" but "abstract".
//
// See also: https://github.com/godotengine/godot/pull/58972
c.godot_params.is_abstract = (!is_instantiable) as sys::GDExtensionBool;

fill_into(
&mut c.godot_params.create_instance_func,
generated_create_fn,
Expand Down
Loading

0 comments on commit cd652b7

Please sign in to comment.