#macro-derive #macro-helpers #proc-macro #macro #helper #inclusive #generics

derive-elves

Writing inclusive derive macros is tedious, this creates provides helper functions that make it easier

3 releases

0.1.2 Mar 26, 2024
0.1.1 Mar 23, 2023
0.1.0 Mar 3, 2023

#275 in Procedural macros

Download history 83/week @ 2024-06-23 79/week @ 2024-06-30 65/week @ 2024-07-07 131/week @ 2024-07-14 90/week @ 2024-07-21 140/week @ 2024-07-28 67/week @ 2024-08-04 51/week @ 2024-08-11 57/week @ 2024-08-18 99/week @ 2024-08-25 195/week @ 2024-09-01 133/week @ 2024-09-08 131/week @ 2024-09-15 134/week @ 2024-09-22 112/week @ 2024-09-29 29/week @ 2024-10-06

416 downloads per month
Used in 4 crates (2 directly)

MIT license

8KB
81 lines

derive-elves

Writing inclusive derive macros is tedious, this creates provides helper functions that make it easier.

type aware impl

The type_aware_impl function makes it easy to write derive macros that take the generics of the underlying type into consideration.

Example

Considering this simple derive macro.

#[proc_macro_derive(Append)]
pub fn push(input_stream: TokenStream) -> TokenStream {
    let input_type = parse_macro_input!(input_stream as DeriveInput);

    let ident = &input_type.ident;

    type_aware_impl(
        quote! {
            impl<T: Append<T>> Append<T> for #ident {
                fn append(&self, l: T) {
                    todo!()
                }
            }
        },
        &input_type,
    )
}

The the following anotated struct,

#[derive(Append)]
struct Foo<S: ToString> {
    bar: S
}

would expand to this:

struct Foo<S: ToString> {
    bar: S
}

impl<T: Append<T>, S: ToString> Append<T> for Foo<S> {
    fn append(&self, l: T) {
        todo!()
    }
}

The above also works for more complex patterns, like the following:

impl Trait for & #ident
impl Trait for &mut #ident
impl Trait for [#ident]
impl Trait for (#ident, A, B, C)

License: MIT

Dependencies

~270–720KB
~17K SLoC