From 77248a7cf7229b5aef1c72f437cca506853ba26c Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Sun, 1 Sep 2024 10:05:33 +0200 Subject: [PATCH] cleanup --- .../derive_order/derive_macros/src/lib.rs | 32 ++++++++----------- research/derive_order/use_macros/src/lib.rs | 8 ++--- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/research/derive_order/derive_macros/src/lib.rs b/research/derive_order/derive_macros/src/lib.rs index 5dc0032..f87b465 100644 --- a/research/derive_order/derive_macros/src/lib.rs +++ b/research/derive_order/derive_macros/src/lib.rs @@ -5,38 +5,32 @@ use syn::{parse_macro_input, DeriveInput}; #[proc_macro_derive(First)] pub fn derive_first(input: TokenStream) -> TokenStream { - let ast = parse_macro_input!(input as DeriveInput); - let name = &ast.ident; + let name = parse_macro_input!(input as DeriveInput).ident; - let expanded = quote! { + TokenStream::from(quote! { impl First for #name { fn is_first_implemented() -> bool { true } } - }; - - TokenStream::from(expanded) + }) } #[proc_macro_derive(Second)] pub fn derive_second(input: TokenStream) -> TokenStream { - let ast = parse_macro_input!(input as DeriveInput); - let name = &ast.ident; + let name = parse_macro_input!(input as DeriveInput).ident; - let expanded = quote! { + TokenStream::from(quote! { impl Second for #name { fn check_first_is_implemented() -> bool { - if <#name as First>::is_first_implemented() { - println!("First is implemented, so Second works!"); - true - } else { - println!("First is NOT implemented, so Second fails!"); - false - } + let result = <#name as First>::is_first_implemented(); + println!( + "First is {}implemented, so Second {}!", + if result { "" } else { "NOT " }, + if result { "works" } else { "fails" } + ); + result } } - }; - - TokenStream::from(expanded) + }) } diff --git a/research/derive_order/use_macros/src/lib.rs b/research/derive_order/use_macros/src/lib.rs index cc8d2ae..0e9383a 100644 --- a/research/derive_order/use_macros/src/lib.rs +++ b/research/derive_order/use_macros/src/lib.rs @@ -12,17 +12,17 @@ mod tests { #[test] fn test_first_second() { - #[derive(First, Second)] // <-- This should work correctly. + #[derive(First, Second)] struct MyStruct; - assert!(::check_first_is_implemented()); + assert!(MyStruct::check_first_is_implemented()); } #[test] fn test_second_first() { - #[derive(Second, First)] // <-- Clarify if this should work or fail. + #[derive(Second, First)] struct MyStruct; - assert!(::check_first_is_implemented()); + assert!(MyStruct::check_first_is_implemented()); } }