Skip to content

Commit d249103

Browse files
committed
Final classes now come without interface trait
1 parent e17836c commit d249103

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

godot-codegen/src/generator/classes.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub fn generate_class_files(
4545
own_notification_enum_name: generated_class.notification_enum.try_to_own_name(),
4646
inherits_macro_ident: generated_class.inherits_macro_ident,
4747
is_pub_sidecar: generated_class.has_sidecar_module,
48+
has_interface_trait: generated_class.has_interface_trait,
4849
});
4950
}
5051

@@ -63,6 +64,7 @@ struct GeneratedClass {
6364
inherits_macro_ident: Option<Ident>,
6465
/// Sidecars are the associated modules with related enum/flag types, such as `node_3d` for `Node3D` class.
6566
has_sidecar_module: bool,
67+
has_interface_trait: bool,
6668
}
6769

6870
struct GeneratedClassModule {
@@ -71,6 +73,7 @@ struct GeneratedClassModule {
7173
own_notification_enum_name: Option<Ident>,
7274
inherits_macro_ident: Option<Ident>,
7375
is_pub_sidecar: bool,
76+
has_interface_trait: bool,
7477
}
7578

7679
struct Construction {
@@ -86,7 +89,6 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
8689
// Strings
8790
let godot_class_str = &class_name.godot_ty;
8891
let class_name_cstr = util::c_str(godot_class_str);
89-
let virtual_trait_str = class_name.virtual_trait_name();
9092

9193
// Idents and tokens
9294
let (base_ty, base_ident_opt) = match class.inherits.as_ref() {
@@ -157,25 +159,33 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
157159
// This checks if token streams (i.e. code) is empty.
158160
let has_sidecar_module = !enums.is_empty() || !builders.is_empty() || has_own_signals;
159161

162+
let module_doc = docs::make_module_doc(class_name);
163+
164+
// Classes that can't be inherited from don't need to provide an interface with overridable virtual methods.
165+
let has_interface_trait = !class.is_final;
166+
let interface_trait = if has_interface_trait {
167+
let virtual_trait_str = class_name.virtual_trait_name();
168+
virtual_traits::make_virtual_methods_trait(
169+
class,
170+
&all_bases,
171+
&virtual_trait_str,
172+
&notification_enum_name,
173+
&cfg_attributes,
174+
view,
175+
)
176+
} else {
177+
TokenStream::new()
178+
};
179+
160180
let class_doc = docs::make_class_doc(
161181
class_name,
162182
base_ident_opt,
163183
notification_enum.is_some(),
164184
has_sidecar_module,
185+
has_interface_trait,
165186
has_own_signals,
166187
);
167188

168-
let module_doc = docs::make_module_doc(class_name);
169-
170-
let virtual_trait = virtual_traits::make_virtual_methods_trait(
171-
class,
172-
&all_bases,
173-
&virtual_trait_str,
174-
&notification_enum_name,
175-
&cfg_attributes,
176-
view,
177-
);
178-
179189
// notify() and notify_reversed() are added after other methods, to list others first in docs.
180190
let notify_methods = notifications::make_notify_methods(class_name, ctx);
181191

@@ -220,7 +230,7 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
220230
// The RawGd<T>'s identity field can be None because of generality (it can represent null pointers, as opposed to Gd<T>).
221231
rtti: Option<crate::private::ObjectRtti>,
222232
}
223-
#virtual_trait
233+
#interface_trait
224234
#notification_enum
225235
impl #class_name {
226236
#constructor
@@ -274,6 +284,7 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
274284
},
275285
inherits_macro_ident,
276286
has_sidecar_module,
287+
has_interface_trait,
277288
}
278289
}
279290

@@ -351,10 +362,14 @@ fn make_class_module_file(classes_and_modules: Vec<GeneratedClassModule>) -> Tok
351362

352363
let vis = is_pub.then_some(quote! { pub });
353364

365+
let interface_reexport = m.has_interface_trait.then(|| {
366+
quote! { pub use #module_name::re_export::#virtual_trait_name; }
367+
});
368+
354369
let class_decl = quote! {
355370
#vis mod #module_name;
356371
pub use #module_name::re_export::#class_name;
357-
pub use #module_name::re_export::#virtual_trait_name;
372+
#interface_reexport
358373
};
359374
class_decls.push(class_decl);
360375

@@ -452,7 +467,7 @@ fn make_constructor_and_default(class: &Class, ctx: &Context) -> Construction {
452467
let final_doc = if class.is_final {
453468
Some(
454469
"\n\n# Final class\n\n\
455-
This class is _final_, meaning you cannot inherit from it.",
470+
This class is _final_, meaning you cannot inherit from it, and it comes without `I*` interface trait.",
456471
)
457472
} else {
458473
None

godot-codegen/src/generator/docs.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn make_class_doc(
1919
base_ident_opt: Option<Ident>,
2020
has_notification_enum: bool,
2121
has_sidecar_module: bool,
22+
has_interface_trait: bool,
2223
has_signal_collection: bool,
2324
) -> String {
2425
let TyName { rust_ty, godot_ty } = class_name;
@@ -62,7 +63,12 @@ pub fn make_class_doc(
6263
godot_ty.to_ascii_lowercase()
6364
);
6465

65-
let trait_name = class_name.virtual_trait_name();
66+
let interface_trait_line = if has_interface_trait {
67+
let trait_name = class_name.virtual_trait_name();
68+
format!("* [`{trait_name}`][crate::classes::{trait_name}]: virtual methods\n")
69+
} else {
70+
String::new()
71+
};
6672

6773
let notes = special_cases::get_class_extra_docs(class_name)
6874
.map(|notes| format!("# Specific notes for this class\n\n{}", notes))
@@ -75,7 +81,7 @@ pub fn make_class_doc(
7581
\
7682
Related symbols:\n\n\
7783
{sidecar_signal_lines}\
78-
* [`{trait_name}`][crate::classes::{trait_name}]: virtual methods\n\
84+
{interface_trait_line}\
7985
{signal_line}\
8086
{notify_line}\
8187
\n\n\

0 commit comments

Comments
 (0)