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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
extern crate proc_macro;
extern crate syn;
extern crate quote;

use proc_macro::{TokenStream};
use syn::{
    parse_macro_input,
    ItemImpl,
    ImplItem,
    Visibility,
    MethodSig,
    Path,
    FnArg,
    Pat,
    PatIdent,
    Ident,
    ImplItemVerbatim,
    ArgCaptured,
};
use syn::spanned::Spanned;
use syn::parse::Error;
use quote::{quote, ToTokens};

/// Make `pub` methods in the annotated `impl Trait for Type` block into
/// inherited methods, which can be used without having to import the trait.
///
/// ```
/// # extern crate inherent_pub;
/// mod module {
///     # use inherent_pub::inherent_pub;
///     pub trait Foo {
///         fn foo(self);
///         fn bar(self);
///     }
///
///     pub struct Bar;
///
///     #[inherent_pub]
///     impl Foo for Bar {
///         // `foo` becomes an inherent method.
///         pub fn foo(self) {}
///         // `bar` is not an inherent method (not `pub`)
///         fn bar(self) {}
///     }
/// }
///
/// fn main() {
///     // We didn't `use foo:Foo`, but we can still use `Bar.foo()`:
///     module::Bar.foo();
///
///     // This does not compile:
///     // bar::Bar.bar();
///
///     {
///         // We need to import the trait in order to use `Bar.bar()`:
///         use module::Foo;
///         module::Bar.bar();
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn inherent_pub(_args: TokenStream, input: TokenStream) -> TokenStream {
    let input: ItemImpl = parse_macro_input!(input as ItemImpl);

    match inherent_pub_impl(input) {
        Ok(output) => {
            output
        },
        Err(error) => {
            error.to_compile_error().into()
        }
    }
}

fn inherent_pub_impl(mut input: ItemImpl) -> Result<TokenStream, Error> {
    let (_, trait_, _) = input.trait_.clone().ok_or_else(|| {
        Error::new(input.span(), "expected `impl <Trait> for <Type>`")
    }).unwrap();

    let methods = extract_pub_methods(&mut input.items);

    let mut result = TokenStream::new().into();
    input.to_tokens(&mut result);

    let pub_impls = methods.into_iter().map(|(vis, sig)| {
        redirect_method(vis, sig, &trait_)
    }).collect::<Result<Vec<_>, Error>>()?;

    let inherent_impl = ItemImpl {
        trait_: None,
        items: pub_impls,
        ..input
    };
    inherent_impl.to_tokens(&mut result);

    Ok(result.into())
}

fn extract_pub_methods(items: &mut Vec<ImplItem>) -> Vec<(Visibility, MethodSig)> {
    items.iter_mut().filter_map(|item| {
        if let ImplItem::Method(method) = item {
            let vis = method.vis.clone();
            let sig = method.sig.clone();
            method.vis = Visibility::Inherited;
            Some((vis, sig))
        } else {
            None
        }
    }).collect()
}

fn redirect_method(vis: Visibility, mut sig: MethodSig, trait_: &Path) -> Result<ImplItem, Error> {
    let mut arg_count: usize = 0;
    let mut args = Vec::new();
    for arg in sig.decl.inputs.iter_mut() {
        match arg {
            FnArg::SelfRef(arg) => {
                let ident = Ident::new("self", arg.span());
                args.push(ident);
            },
            FnArg::SelfValue(arg) => {
                arg.mutability = None;
                let ident = Ident::new("self", arg.span());
                args.push(ident);
            },
            FnArg::Captured(ArgCaptured { pat: Pat::Ident(pat_ident), .. }) if args.is_empty() && pat_ident.ident.to_string() == "self" => {
                pat_ident.mutability = None;
                let ident = Ident::new("self", pat_ident.span());
                args.push(ident);
            },
            FnArg::Captured(arg) => {
                arg_count += 1;
                let ident = Ident::new(&format!("arg{}", arg_count), arg.span());
                arg.pat = Pat::Ident(PatIdent {
                    by_ref: None,
                    mutability: None,
                    ident: ident.clone(),
                    subpat: None,
                });
                args.push(ident);
            },
            FnArg::Inferred(_) => {
                return Err(Error::new(arg.span(), "inherent_pub does not know how to handle inferred arguments"))
            },
            FnArg::Ignored(_) => {
                return Err(Error::new(arg.span(), "inherent_pub does not know how to handle ignored arguments"))
            },
        }
    }
    let fn_name = &sig.ident;
    Ok(ImplItem::Verbatim(ImplItemVerbatim { tts: quote!(
        #[doc(hidden)]
        #[inline(always)]
        #vis #sig {
            <Self as #trait_>::#fn_name(#(#args),*)
        }
    )}))
}