const_identify_derive/
lib.rs

1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{parse_macro_input, DeriveInput};
4
5#[proc_macro_derive(ConstIdentify)]
6pub fn derive_const_identify(input: TokenStream) -> TokenStream {
7    // parse the input
8    let DeriveInput {
9        ident, generics, ..
10    } = parse_macro_input!(input);
11
12    // gather generics for quote
13    let (implgen, typegen, wheregen) = generics.split_for_impl();
14
15    // create output
16    let output = quote! {
17        unsafe impl #implgen ::const_identify::ConstIdentify for #ident #typegen #wheregen {
18            const CONST_ID: ::const_identify::ConstId = ::const_identify::ConstId::generate(
19                concat!(
20                    module_path!(), "::", stringify!(#ident),
21                    ":", file!(), ":", line!(), ":", column!()
22                )
23            );
24        }
25    };
26
27    // convert output and return
28    output.into()
29}