-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1a97f6
commit 377becd
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[workspace] | ||
members = [ | ||
"derive_macros", | ||
"use_macros", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "derive_macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = "2.0" | ||
quote = "1.0" | ||
proc-macro2 = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
extern crate proc_macro; | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
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 expanded = 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 expanded = 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 | ||
} | ||
} | ||
} | ||
}; | ||
|
||
TokenStream::from(expanded) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "use_macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
derive_macros = { path = "../derive_macros" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use derive_macros::{First, Second}; | ||
|
||
trait First { | ||
fn is_first_implemented() -> bool; | ||
} | ||
|
||
trait Second { | ||
fn check_first_is_implemented() -> bool; | ||
} | ||
|
||
#[test] | ||
fn test_first_second() { | ||
#[derive(First, Second)] // <-- This should work correctly. | ||
struct MyStruct; | ||
|
||
assert!(<MyStruct as Second>::check_first_is_implemented()); | ||
} | ||
|
||
#[test] | ||
fn test_second_first() { | ||
#[derive(Second, First)] // <-- Clarify if this should work or fail. | ||
struct MyStruct; | ||
|
||
assert!(<MyStruct as Second>::check_first_is_implemented()); | ||
} | ||
} |