-
Notifications
You must be signed in to change notification settings - Fork 17
/
lib.rs
29 lines (26 loc) · 830 Bytes
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_derive(CustomBits)]
pub fn custom_bits_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_custom_bits_macro(&ast)
}
fn impl_custom_bits_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let fields_number = match &ast.data {
syn::Data::Struct(d) => d.fields.len(),
syn::Data::Enum(d) => d.variants.len(),
syn::Data::Union(d) => d.fields.named.len(),
};
let gen = quote! {
impl CustomBits for #name {
fn fields(&self) -> usize{
#fields_number
}
}
};
gen.into()
}