Skip to content

Commit

Permalink
Merge #147
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bors[bot] and mio991 authored Mar 7, 2023
2 parents 7d42ebb + 67a68e4 commit fb15aef
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
6 changes: 6 additions & 0 deletions godot-core/src/builtin/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ pub trait VariantMetadata {
}
}

impl<T: VariantMetadata> VariantMetadata for Option<T> {
fn variant_type() -> VariantType {
T::variant_type()
}
}

// ----------------------------------------------------------------------------------------------------------------------------------------------

/// Rusty abstraction of sys::GDExtensionPropertyInfo
Expand Down
15 changes: 7 additions & 8 deletions godot-macros/src/derive_godot_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ struct Fields {

struct Field {
name: Ident,
_ty: TyExpr,
ty: TyExpr,
}

impl Field {
fn new(field: &NamedField) -> Self {
Self {
name: field.name.clone(),
_ty: field.ty.clone(),
ty: field.ty.clone(),
}
}
}
Expand All @@ -176,20 +176,17 @@ struct ExportedField {
field: Field,
getter: String,
setter: String,
variant_type: String,
}

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

Ok(ExportedField {
field,
getter,
setter,
variant_type,
})
}
}
Expand Down Expand Up @@ -249,12 +246,14 @@ fn make_exports_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
let name = exported_field.field.name.to_string();
let getter = proc_macro2::Literal::from_str(&exported_field.getter).unwrap();
let setter = proc_macro2::Literal::from_str(&exported_field.setter).unwrap();
let vtype = &exported_field.variant_type;
let variant_type: TokenStream = vtype[1..vtype.len() - 1].parse().unwrap();
let field_type = exported_field.field.ty.clone();

quote! {
use ::godot::builtin::meta::VariantMetadata;

let class_name = ::godot::builtin::StringName::from(#class_name::CLASS_NAME);
let property_info = ::godot::builtin::meta::PropertyInfo::new(
#variant_type,
<#field_type>::variant_type(),
::godot::builtin::meta::ClassName::of::<#class_name>(),
::godot::builtin::StringName::from(#name),
);
Expand Down
18 changes: 3 additions & 15 deletions itest/rust/src/export_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,11 @@ use godot::prelude::*;
struct HasProperty {
#[base]
base: Base<Node>,
#[export(
getter = "get_int_val",
setter = "set_int_val",
variant_type = "::godot::sys::VariantType::Int"
)]
#[export(getter = "get_int_val", setter = "set_int_val")]
int_val: i32,
#[export(
getter = "get_string_val",
setter = "set_string_val",
variant_type = "::godot::sys::VariantType::String"
)]
#[export(getter = "get_string_val", setter = "set_string_val")]
string_val: GodotString,
#[export(
getter = "get_object_val",
setter = "set_object_val",
variant_type = "::godot::sys::VariantType::Object"
)]
#[export(getter = "get_object_val", setter = "set_object_val")]
object_val: Option<Gd<Object>>,
}

Expand Down

0 comments on commit fb15aef

Please sign in to comment.