summaryrefslogtreecommitdiffstats
path: root/libs/hello_macro/hello_macro_derive/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'libs/hello_macro/hello_macro_derive/src/lib.rs')
-rw-r--r--libs/hello_macro/hello_macro_derive/src/lib.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/libs/hello_macro/hello_macro_derive/src/lib.rs b/libs/hello_macro/hello_macro_derive/src/lib.rs
new file mode 100644
index 0000000..1eaef1f
--- /dev/null
+++ b/libs/hello_macro/hello_macro_derive/src/lib.rs
@@ -0,0 +1,24 @@
+use proc_macro::TokenStream;
+use quote::quote;
+
+#[proc_macro_derive(HelloMacro)]
+pub fn hello_macro_derive(input: TokenStream) -> TokenStream {
+ // Construct a representation of Rust code as a syntax tree
+ // that we can manipulate.
+ let ast = syn::parse(input).unwrap();
+
+ // Build the trait implementation
+ impl_hello_macro(&ast)
+}
+
+fn impl_hello_macro(ast: &syn::DeriveInput) -> TokenStream {
+ let name = &ast.ident;
+ let generated = quote! {
+ impl HelloMacro for #name {
+ fn hello_macro() {
+ println!("Hello, Macro! My name is {}!", stringify!(#name));
+ }
+ }
+ };
+ generated.into()
+}