]> git.proxmox.com Git - rustc.git/blob - src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/src/lib.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / src / doc / book / listings / ch19-advanced-features / no-listing-21-pancakes / hello_macro / hello_macro_derive / src / lib.rs
1 use proc_macro::TokenStream;
2 use quote::quote;
3 use syn;
4
5 #[proc_macro_derive(HelloMacro)]
6 pub fn hello_macro_derive(input: TokenStream) -> TokenStream {
7 // Construct a representation of Rust code as a syntax tree
8 // that we can manipulate
9 let ast = syn::parse(input).unwrap();
10
11 // Build the trait implementation
12 impl_hello_macro(&ast)
13 }
14
15 fn impl_hello_macro(ast: &syn::DeriveInput) -> TokenStream {
16 let name = &ast.ident;
17 let gen = quote! {
18 impl HelloMacro for #name {
19 fn hello_macro() {
20 println!("Hello, Macro! My name is {}!", stringify!(#name));
21 }
22 }
23 };
24 gen.into()
25 }