Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym-arutyunyan committed Sep 1, 2024
1 parent 377becd commit 77248a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
32 changes: 13 additions & 19 deletions research/derive_order/derive_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
8 changes: 4 additions & 4 deletions research/derive_order/use_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ mod tests {

#[test]
fn test_first_second() {
#[derive(First, Second)] // <-- This should work correctly.
#[derive(First, Second)]
struct MyStruct;

assert!(<MyStruct as Second>::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!(<MyStruct as Second>::check_first_is_implemented());
assert!(MyStruct::check_first_is_implemented());
}
}

0 comments on commit 77248a7

Please sign in to comment.