Skip to content

Commit fb15aef

Browse files
bors[bot]mio991
andauthored
Merge #147
147: #[export] infer `VariantType` r=Bromeon a=mio991 I removed the explicit `VariantType` declaration and replaced it with the ability to infer the same. Also added an implementation for `VariantMetadata` for `Option` not sure if this is good but it was necessary. Co-authored-by: mio991 <[email protected]>
2 parents 7d42ebb + 67a68e4 commit fb15aef

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

godot-core/src/builtin/meta/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ pub trait VariantMetadata {
3939
}
4040
}
4141

42+
impl<T: VariantMetadata> VariantMetadata for Option<T> {
43+
fn variant_type() -> VariantType {
44+
T::variant_type()
45+
}
46+
}
47+
4248
// ----------------------------------------------------------------------------------------------------------------------------------------------
4349

4450
/// Rusty abstraction of sys::GDExtensionPropertyInfo

godot-macros/src/derive_godot_class.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ struct Fields {
160160

161161
struct Field {
162162
name: Ident,
163-
_ty: TyExpr,
163+
ty: TyExpr,
164164
}
165165

166166
impl Field {
167167
fn new(field: &NamedField) -> Self {
168168
Self {
169169
name: field.name.clone(),
170-
_ty: field.ty.clone(),
170+
ty: field.ty.clone(),
171171
}
172172
}
173173
}
@@ -176,20 +176,17 @@ struct ExportedField {
176176
field: Field,
177177
getter: String,
178178
setter: String,
179-
variant_type: String,
180179
}
181180

182181
impl ExportedField {
183182
pub fn new_from_kv(field: Field, parser: &mut KvParser) -> ParseResult<ExportedField> {
184183
let getter = parser.handle_lit_required("getter")?;
185184
let setter = parser.handle_lit_required("setter")?;
186-
let variant_type = parser.handle_lit_required("variant_type")?;
187185

188186
Ok(ExportedField {
189187
field,
190188
getter,
191189
setter,
192-
variant_type,
193190
})
194191
}
195192
}
@@ -249,12 +246,14 @@ fn make_exports_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
249246
let name = exported_field.field.name.to_string();
250247
let getter = proc_macro2::Literal::from_str(&exported_field.getter).unwrap();
251248
let setter = proc_macro2::Literal::from_str(&exported_field.setter).unwrap();
252-
let vtype = &exported_field.variant_type;
253-
let variant_type: TokenStream = vtype[1..vtype.len() - 1].parse().unwrap();
249+
let field_type = exported_field.field.ty.clone();
250+
254251
quote! {
252+
use ::godot::builtin::meta::VariantMetadata;
253+
255254
let class_name = ::godot::builtin::StringName::from(#class_name::CLASS_NAME);
256255
let property_info = ::godot::builtin::meta::PropertyInfo::new(
257-
#variant_type,
256+
<#field_type>::variant_type(),
258257
::godot::builtin::meta::ClassName::of::<#class_name>(),
259258
::godot::builtin::StringName::from(#name),
260259
);

itest/rust/src/export_test.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,11 @@ use godot::prelude::*;
1313
struct HasProperty {
1414
#[base]
1515
base: Base<Node>,
16-
#[export(
17-
getter = "get_int_val",
18-
setter = "set_int_val",
19-
variant_type = "::godot::sys::VariantType::Int"
20-
)]
16+
#[export(getter = "get_int_val", setter = "set_int_val")]
2117
int_val: i32,
22-
#[export(
23-
getter = "get_string_val",
24-
setter = "set_string_val",
25-
variant_type = "::godot::sys::VariantType::String"
26-
)]
18+
#[export(getter = "get_string_val", setter = "set_string_val")]
2719
string_val: GodotString,
28-
#[export(
29-
getter = "get_object_val",
30-
setter = "set_object_val",
31-
variant_type = "::godot::sys::VariantType::Object"
32-
)]
20+
#[export(getter = "get_object_val", setter = "set_object_val")]
3321
object_val: Option<Gd<Object>>,
3422
}
3523

0 commit comments

Comments
 (0)