From c92409e918498ff1bc38471e66a9b20b278eb15a Mon Sep 17 00:00:00 2001 From: patataofcourse Date: Fri, 4 Aug 2023 01:31:44 +0200 Subject: [PATCH] review fixing --- godot-macros/src/derive_export.rs | 17 ++++++----------- godot-macros/src/lib.rs | 14 +++++++------- itest/rust/src/property_test.rs | 1 - 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/godot-macros/src/derive_export.rs b/godot-macros/src/derive_export.rs index 9cead80e2..21f05270a 100644 --- a/godot-macros/src/derive_export.rs +++ b/godot-macros/src/derive_export.rs @@ -25,14 +25,15 @@ pub fn transform(decl: Declaration) -> ParseResult { _ => unreachable!(), }; - let mut hint_string = String::new(); + let hint_string; if enum_.variants.is_empty() { return bail!( enum_.name, "In order to derive Export, enums must have at least one variant" ); } else { - for (i, (enum_v, _)) in enum_.variants.inner.iter().enumerate() { + let mut hint_string_segments = Vec::new(); + for (enum_v, _) in enum_.variants.inner.iter() { let v_name = enum_v.name.clone(); let v_disc = if let Some(c) = enum_v.value.clone() { c.value @@ -42,16 +43,9 @@ pub fn transform(decl: Declaration) -> ParseResult { "Property can only be derived on enums with explicit discriminants in all their variants" ); }; + let v_disc_trimmed = v_disc.to_string().trim_matches(['(', ')'].as_slice()).to_string(); - hint_string += &format!( - "{v_name}:{}{}", - v_disc.to_string().trim_matches(['(', ')'].as_slice()), - if i < enum_.variants.inner.len() - 1 { - "," - } else { - "" - } - ); + hint_string_segments.push(format!("{v_name}:{v_disc_trimmed}")); match &enum_v.contents { StructFields::Unit => {} @@ -63,6 +57,7 @@ pub fn transform(decl: Declaration) -> ParseResult { } }; } + hint_string = hint_string_segments.join(","); } let out = quote! { diff --git a/godot-macros/src/lib.rs b/godot-macros/src/lib.rs index 77ff20e71..ec38a36d7 100644 --- a/godot-macros/src/lib.rs +++ b/godot-macros/src/lib.rs @@ -464,11 +464,11 @@ pub fn derive_from_variant(input: TokenStream) -> TokenStream { /// - This will likely stay this way, since `isize`, the default repr type, is not a concept in Godot. /// - The enum variants must not have any fields - currently only unit variants are supported. /// - The enum variants must have explicit discriminants, that is, e.g. `A = 2`, not just `A` -/// - This will likely stay this way in order to keep proc-macro dependencies lighter. /// -/// Example : +/// # Example /// -/// ```ignore +/// ```no_run +/// # use godot::prelude::*; /// #[repr(i32)] /// #[derive(Debug, Property)] /// enum TestEnum { @@ -482,11 +482,12 @@ pub fn derive_from_variant(input: TokenStream) -> TokenStream { /// foo: TestEnum /// } /// -/// // This would not panic. +/// # fn main() { /// let mut class = TestClass {foo: TestEnum::B}.get_foo(); -/// assert!(class.get_foo() == TestEnum::B as i32); +/// assert_eq!(class.get_foo(), TestEnum::B as i32); /// class.set_foo(TestEnum::A); -/// assert!(class.foo == TestEnum::A as i32); +/// assert_eq!(class.foo, TestEnum::A as i32); +/// # } /// ``` #[proc_macro_derive(Property)] pub fn derive_property(input: TokenStream) -> TokenStream { @@ -499,7 +500,6 @@ pub fn derive_property(input: TokenStream) -> TokenStream { /// - Only works for enums, structs aren't supported by this derive macro at the moment. /// - The enum variants must not have any fields - currently only unit variants are supported. /// - The enum variants must have explicit discriminants, that is, e.g. `A = 2`, not just `A` -/// - This will likely stay this way in order to keep proc-macro dependencies lighter. /// ``` #[proc_macro_derive(Export)] pub fn derive_export(input: TokenStream) -> TokenStream { diff --git a/itest/rust/src/property_test.rs b/itest/rust/src/property_test.rs index 2c9265519..cab166b12 100644 --- a/itest/rust/src/property_test.rs +++ b/itest/rust/src/property_test.rs @@ -348,7 +348,6 @@ fn derive_export() { let class: Gd = Gd::new_default(); let property = class - .bind() .get_property_list() .iter_shared() .find(|c| c.get_or_nil("name") == "foo".to_variant())