Proc_macro_roids 0.1.0 published

#1

Hiya, have just published proc_macro_roids, a crate that provide traits and functions to make writing proc macros more ergonomic.

Links:

Motivation:

Imagine a world where proc macros were easy.

Well, hopefully you don’t have to imagine any longer!

Example:

  1. Append derives.

    #[proc_macro_attribute]
    pub fn copy(_args: TokenStream, item: TokenStream) -> TokenStream {
        // Example input:
        //
        // #[copy]
        // #[derive(Debug)]
        // struct Struct;
        let mut ast = parse_macro_input!(item as DeriveInput);
    
        let derives = parse_quote!(Clone, Copy);
        ast.append_derives(derives);
    
        // Example output:
        //
        // #[derive(Debug, Clone, Copy)]
        // struct Struct;
        TokenStream::from(quote! { #ast })
    }
    
  2. Append fields.

    #[proc_macro_attribute]
    pub fn append_fields(_args: TokenStream, item: TokenStream) -> TokenStream {
        // Example input:
        // #[append_fields]
        // struct StructNamed { a: u32, b: i32 }
        let mut ast = parse_macro_input!(item as DeriveInput);
    
        let fields_additional: FieldsNamed = parse_quote!({ c: i64, d: usize });
        ast.append(fields_additional);
    
        // Example output:
        // struct StructNamed { a: u32, b: i32, c: i64, d: usize }
        TokenStream::from(quote! { #ast })
    }
    

More examples are in the README.md and docs.

2 Likes
What's everyone working on this week (14/2019)?
#2

I like it a lot!

These are the kind of little helpers that make writing macro_rules-like proc_macros easier: proc_macros are too cumbersome to just use as minor shorthands (e.g., creating a custom basic derive Deref or stuff like that), and I personally end up just using macro_rules! for that although the usage site gets much uglier.

For instance, compare

  • #[newtype_deref]
    struct Name(&'static str);
    

with

  • newtype_deref! {
        struct Name(&'static str);
    }
    
1 Like
#3

Cool thanks! Have fixed those issues (oh no, just realized I missed one), and also got code coverage being reported

1 Like