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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemStruct, Meta, NestedMeta};

/// The whole point.
///
/// Use this macro as a shorthand for a negative impl and to confuse
/// your coworkers who will definitely misread it upon first sight:
///
/// ```
/// #![feature(negative_impls)]
/// use deprive::deprive;
///
/// #[deprive(Send, Sync)]
/// struct X {}
/// ```
///
/// The above will expand to:
///
/// ```
/// #![feature(negative_impls)]
/// struct X {}
/// impl !Send for X {}
/// impl !Sync for X {}
/// ```
#[proc_macro_attribute]
pub fn deprive(attr: TokenStream, input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);
    let attr = parse_macro_input!(attr as syn::AttributeArgs);

    let id = input.ident.clone();

    let impls = attr
        .iter()
        .map(|trayt| match trayt {
            NestedMeta::Meta(Meta::Path(trayt)) => {
                quote! {
                    impl ! #trayt for #id {}
                }
            }
            _ => {
                panic!("Unsupported");
            }
        })
        .fold(quote! {}, |acc, new| quote! {#acc #new});

    let expanded = quote! {
        #input

        #impls
    };

    TokenStream::from(expanded)
}