derive-elves

Crates.ioderive-elves
lib.rsderive-elves
version0.1.2
sourcesrc
created_at2023-03-03 12:08:10.015189
updated_at2024-03-26 21:52:06.521531
descriptionWriting inclusive derive macros is tedious, this creates provides helper functions that make it easier.
homepage
repositoryhttps://github.com/michaelvanstraten/derive-elves
max_upload_size
id799702
size23,951
Michael (michaelvanstraten)

documentation

https://docs.rs/derive-elves/

README

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

Commit count: 11

cargo fmt