- Sample code:
#![crate_type = "proc-macro"]
#![feature(extern_crate_item_prelude)]
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
#[proc_macro_derive(TypeName)]
pub fn type_name(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_typename(&ast).into()
}
fn impl_typename(ast: &syn::DeriveInput) -> quote::Tokens {
let name = &ast.ident;
quote! {
impl TypeName for #name {
fn typename() -> String {
stringify!(#name).to_string()
}
}
}
}
-
The above code is from a Rust book I am reading. I am not familiar with procedural macros, and am not sure if (1) I'm doing something wrong or (2) if the code is outdated. Either way, pointing out what's wrong with the code / how to fix it would be helpful.
-
I am familiar with Rust macro-rules. I am also familiar with Lisp style macros, so the
quote!
looks like lisp 'quasi-quote' and the#
looks like Lisp unquote.