derive_elves/lib.rs
1/*!
2Writing inclusive derive macros is tedious,
3this creates provides helper functions that make it easier.
4
5# type aware impl
6The `type_aware_impl` function makes it easy to write derive macros
7that take the generics of the underlying type into consideration.
8## Example
9Considering this simple derive macro.
10```ignore
11#[proc_macro_derive(Append)]
12pub fn push(input_stream: TokenStream) -> TokenStream {
13 let input_type = parse_macro_input!(input_stream as DeriveInput);
14
15 let ident = &input_type.ident;
16
17 type_aware_impl(
18 quote! {
19 impl<T: Append<T>> Append<T> for #ident {
20 fn append(&self, l: T) {
21 todo!()
22 }
23 }
24 },
25 &input_type,
26 )
27}
28```
29The the following anotated struct,
30```ignore
31#[derive(Append)]
32struct Foo<S: ToString> {
33 bar: S
34}
35```
36would expand to this:
37```ignore
38struct Foo<S: ToString> {
39 bar: S
40}
41
42impl<T: Append<T>, S: ToString> Append<T> for Foo<S> {
43 fn append(&self, l: T) {
44 todo!()
45 }
46}
47```
48The above also works for more complex patterns,
49like the following:
50```ignore
51impl Trait for & #ident
52```
53```ignore
54impl Trait for &mut #ident
55```
56```ignore
57impl Trait for [#ident]
58```
59```ignore
60impl Trait for (#ident, A, B, C)
61```
62*/
63
64pub use type_aware_impl::*;
65
66mod type_aware_impl;