Skip to content

Commit 128c7ce

Browse files
authored
Merge pull request #844 from godot-rust/qol/init-val
Shorten `#[init(default = ...)]` to `#[init(val = ...)]`
2 parents 2d052a6 + 2528395 commit 128c7ce

File tree

9 files changed

+51
-23
lines changed

9 files changed

+51
-23
lines changed

godot-core/src/deprecated.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@ macro_rules! emit_deprecated_warning {
3535
pub use crate::emit_deprecated_warning;
3636

3737
// ----------------------------------------------------------------------------------------------------------------------------------------------
38-
// Concrete deprecations
38+
// Library-side deprecations
39+
40+
#[deprecated = "\nThe attribute key #[init(val = ...)] replaces #[init(default = ...)].\n\
41+
More information on https://github.com/godot-rust/gdext/pull/844."]
42+
pub const fn init_default() {}
43+
44+
// ----------------------------------------------------------------------------------------------------------------------------------------------
45+
// Godot-side deprecations
3946

4047
// This is a Godot-side deprecation. Since it's the only way in Godot 4.1, we keep compatibility for now.
4148
#[cfg_attr(
4249
since_api = "4.2",
43-
deprecated = "Use #[export(range = (radians_as_degrees))] and not #[export(range = (radians))]. \n\
50+
deprecated = "\nUse #[export(range = (radians_as_degrees))] and not #[export(range = (radians))].\n\
4451
More information on https://github.com/godotengine/godot/pull/82195."
4552
)]
4653
pub const fn export_range_radians() {}

godot-core/src/obj/onready.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use std::mem;
8787
/// base: Base<Node>,
8888
/// #[init(node = "ChildPath")]
8989
/// auto: OnReady<Gd<Node2D>>,
90-
/// #[init(default = OnReady::manual())]
90+
/// #[init(val = OnReady::manual())]
9191
/// manual: OnReady<i32>,
9292
/// }
9393
///

godot-macros/src/class/data_models/field.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use proc_macro2::{Ident, TokenStream};
1111
pub struct Field {
1212
pub name: Ident,
1313
pub ty: venial::TypeExpr,
14-
pub default: Option<TokenStream>,
14+
pub default_val: Option<TokenStream>,
1515
pub var: Option<FieldVar>,
1616
pub export: Option<FieldExport>,
1717
pub is_onready: bool,
@@ -24,7 +24,7 @@ impl Field {
2424
Self {
2525
name: field.name.clone(),
2626
ty: field.ty.clone(),
27-
default: None,
27+
default_val: None,
2828
var: None,
2929
export: None,
3030
is_onready: false,
@@ -40,4 +40,7 @@ pub struct Fields {
4040

4141
/// The field with type `Base<T>`, if available.
4242
pub base_field: Option<Field>,
43+
44+
/// Deprecation warnings.
45+
pub deprecations: Vec<TokenStream>,
4346
}

godot-macros/src/class/derive_godot_class.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub fn derive_godot_class(item: venial::Item) -> ParseResult<TokenStream> {
7575
let mut create_fn = quote! { None };
7676
let mut recreate_fn = quote! { None };
7777
let mut is_instantiable = true;
78+
let deprecations = &fields.deprecations;
7879

7980
match struct_cfg.init_strategy {
8081
InitStrategy::Generated => {
@@ -137,6 +138,7 @@ pub fn derive_godot_class(item: venial::Item) -> ParseResult<TokenStream> {
137138
#godot_exports_impl
138139
#user_class_impl
139140
#init_expecter
141+
#( #deprecations )*
140142

141143
::godot::sys::plugin_add!(__GODOT_PLUGIN_REGISTRY in #prv; #prv::ClassPlugin {
142144
class_name: #class_name_obj,
@@ -214,7 +216,7 @@ fn make_godot_init_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
214216
let rest_init = fields.all_fields.iter().map(|field| {
215217
let field_name = field.name.clone();
216218
let value_expr = field
217-
.default
219+
.default_val
218220
.clone()
219221
.unwrap_or_else(|| quote! { ::std::default::Default::default() });
220222

@@ -399,6 +401,7 @@ fn parse_fields(
399401
) -> ParseResult<Fields> {
400402
let mut all_fields = vec![];
401403
let mut base_field = Option::<Field>::None;
404+
let mut deprecations = vec![];
402405

403406
// Attributes on struct fields
404407
for (named_field, _punct) in named_fields {
@@ -425,9 +428,23 @@ fn parse_fields(
425428
);
426429
}
427430

428-
// #[init(default = expr)]
431+
// #[init(val = expr)]
432+
if let Some(default) = parser.handle_expr("val")? {
433+
field.default_val = Some(default);
434+
}
435+
436+
// Deprecated #[init(default = expr)]
429437
if let Some(default) = parser.handle_expr("default")? {
430-
field.default = Some(default);
438+
if field.default_val.is_some() {
439+
return bail!(
440+
parser.span(),
441+
"Cannot use both `val` and `default` keys in #[init]; prefer using `val`"
442+
);
443+
}
444+
field.default_val = Some(default);
445+
deprecations.push(quote! {
446+
::godot::__deprecated::emit_deprecated_warning!(init_default);
447+
})
431448
}
432449

433450
// #[init(node = "NodePath")]
@@ -437,22 +454,22 @@ fn parse_fields(
437454
parser.span(),
438455
"The key `node` in attribute #[init] requires field of type `OnReady<T>`\n\
439456
Help: The syntax #[init(node = \"NodePath\")] is equivalent to \
440-
#[init(default = OnReady::node(\"NodePath\"))], \
457+
#[init(val = OnReady::node(\"NodePath\"))], \
441458
which can only be assigned to fields of type `OnReady<T>`"
442459
);
443460
}
444461

445-
if field.default.is_some() {
462+
if field.default_val.is_some() {
446463
return bail!(
447464
parser.span(),
448465
"The key `node` in attribute #[init] is mutually exclusive with the key `default`\n\
449466
Help: The syntax #[init(node = \"NodePath\")] is equivalent to \
450-
#[init(default = OnReady::node(\"NodePath\"))], \
467+
#[init(val = OnReady::node(\"NodePath\"))], \
451468
both aren't allowed since they would override each other"
452469
);
453470
}
454471

455-
field.default = Some(quote! {
472+
field.default_val = Some(quote! {
456473
OnReady::node(#node_path)
457474
});
458475
}
@@ -491,7 +508,7 @@ fn parse_fields(
491508
if field.is_onready
492509
|| field.var.is_some()
493510
|| field.export.is_some()
494-
|| field.default.is_some()
511+
|| field.default_val.is_some()
495512
{
496513
return bail!(
497514
named_field,
@@ -516,6 +533,7 @@ fn parse_fields(
516533
Ok(Fields {
517534
all_fields,
518535
base_field,
536+
deprecations,
519537
})
520538
}
521539

godot-macros/src/docs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn member(member: &Field) -> Option<String> {
167167
let docs = make_docs_from_attributes(&member.attributes)?;
168168
let name = &member.name;
169169
let ty = member.ty.to_token_stream().to_string();
170-
let default = member.default.to_token_stream().to_string();
170+
let default = member.default_val.to_token_stream().to_string();
171171
Some(format!(
172172
r#"<member name="{name}" type="{ty}" default="{default}">{docs}</member>"#
173173
))

godot-macros/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ use crate::util::ident;
6767
/// ```
6868
///
6969
/// The generated `init` function will initialize each struct field (except the field of type `Base<T>`, if any)
70-
/// using `Default::default()`. To assign some other value, annotate the field with `#[init(default = ...)]`:
70+
/// using `Default::default()`. To assign some other value, annotate the field with `#[init(val = ...)]`:
7171
///
7272
/// ```
7373
/// # use godot_macros::GodotClass;
7474
/// #[derive(GodotClass)]
7575
/// #[class(init)]
7676
/// struct MyStruct {
77-
/// #[init(default = 42)]
77+
/// #[init(val = 42)]
7878
/// my_field: i64
7979
/// }
8080
/// ```
@@ -91,7 +91,7 @@ use crate::util::ident;
9191
/// # #[derive(GodotClass)]
9292
/// # #[class(init)]
9393
/// # struct MyStruct {
94-
/// #[init(default = (HashMap::<i64, i64>::new()))]
94+
/// #[init(val = (HashMap::<i64, i64>::new()))]
9595
/// // ^ parentheses needed due to this comma
9696
/// my_field: HashMap<i64, i64>,
9797
/// # }
@@ -544,7 +544,7 @@ pub fn derive_godot_class(input: TokenStream) -> TokenStream {
544544
///
545545
/// ## Generated `init`
546546
///
547-
/// This initializes the `Base<T>` field, and every other field with either `Default::default()` or the value specified in `#[init(default = ...)]`.
547+
/// This initializes the `Base<T>` field, and every other field with either `Default::default()` or the value specified in `#[init(val = ...)]`.
548548
///
549549
/// ```no_run
550550
/// # use godot::prelude::*;
@@ -553,7 +553,7 @@ pub fn derive_godot_class(input: TokenStream) -> TokenStream {
553553
/// pub struct MyNode {
554554
/// base: Base<Node>,
555555
///
556-
/// #[init(default = 42)]
556+
/// #[init(val = 42)]
557557
/// some_integer: i64,
558558
/// }
559559
/// ```

itest/rust/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ fn generate_property_template(inputs: &[Input]) -> PropertyTests {
421421

422422
let initializer = initializer
423423
.as_ref()
424-
.map(|init| quote! { #[init(default = #init)] });
424+
.map(|init| quote! { #[init(val = #init)] });
425425

426426
rust.extend([
427427
quote! {

itest/rust/src/object_tests/onready_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ struct InitWithNodeOrBase {
273273
base: Base<Node>,
274274
#[init(node = "child")]
275275
node: OnReady<Gd<Node>>,
276-
#[init(default = OnReady::from_base_fn(|b| b.get_name().to_string()))]
276+
#[init(val = OnReady::from_base_fn(|b| b.get_name().to_string()))]
277277
self_name: OnReady<String>,
278278
}
279279

itest/rust/src/register_tests/var_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ struct WithInitDefaults {
1414
default_int: i64,
1515

1616
#[var(get)]
17-
#[init(default = 42)]
17+
#[init(val = 42)]
1818
literal_int: i64,
1919

2020
#[var(get)]
21-
#[init(default = -42)]
21+
#[init(val = -42)]
2222
expr_int: i64,
2323
}

0 commit comments

Comments
 (0)