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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![allow(nonstandard_style, unused_imports)]
use ::core::{
ops::Not as _,
};
use ::proc_macro::{
TokenStream,
};
use ::proc_macro2::{
Span,
TokenStream as TokenStream2,
TokenTree as TT,
};
use ::quote::{
format_ident,
quote,
quote_spanned,
ToTokens,
};
use ::syn::{*,
parse::{Parse, Parser, ParseStream},
punctuated::Punctuated,
spanned::Spanned,
Result,
};
#[proc_macro_attribute] pub
fn extension (
attrs: TokenStream,
input: TokenStream,
) -> TokenStream
{
extension_impl(attrs.into(), input.into())
.unwrap_or_else(|err| {
let mut errors =
err .into_iter()
.map(|err| Error::new(
err.span(),
format_args!("`#[extension(trait …)]`: {}", err),
))
;
let mut err = errors.next().unwrap();
errors.for_each(|cur| err.combine(cur));
err.to_compile_error()
})
.into()
}
struct Attrs {
pub_: Visibility,
trait_: Token![trait],
TraitName: Ident,
}
impl Parse for Attrs {
fn parse (input: ParseStream<'_>)
-> Result<Attrs>
{
Ok(Self {
pub_: input.parse()?,
trait_: input.parse()?,
TraitName: input.parse()?,
})
}
}
#[cfg(any())]
const _: () = {
use ::ext_trait::extension;
#[extension(trait disregard_err)]
impl<T, E> Result<T, E> {
fn disregard_err(self) -> Option<T> { self }
}
};
fn extension_impl (
attrs: TokenStream2,
input: TokenStream2,
) -> Result<TokenStream2>
{
let trait_def_span = attrs.span();
let Attrs { pub_, trait_, TraitName } = parse2(attrs)?;
let ref mut item_impl: ItemImpl = parse2(input)?;
let (intro_generics, fwd_generics, where_clause) = item_impl.generics.split_for_impl();
match Option::replace(
&mut item_impl.trait_,
(None, parse_quote!( #TraitName #fwd_generics ), <_>::default()),
)
{
| Some((_, _, extraneous_for)) => return Err(Error::new_spanned(
extraneous_for,
"expected inherent `impl<…> Type<…>` syntax",
)),
| _ => {},
}
let ref item_impl = item_impl;
let each_entry = item_impl.items.iter().map(|it| Ok(match it {
| ImplItem::Const(ImplItemConst {
vis: pub_,
defaultness: default_,
const_token: const_,
ident: CONST_NAME @ _,
ty: Ty @ _,
..
}) => quote!(
#pub_
#default_
#const_ #CONST_NAME: #Ty;
),
| ImplItem::Method(ImplItemMethod {
vis: pub_,
defaultness: default_,
sig,
..
}) => {
let mut sig = sig.clone();
sig.inputs.iter_mut().for_each(|fn_arg| match fn_arg {
| FnArg::Receiver(Receiver { reference, mutability, .. }) => {
if reference.is_none() {
*mutability = None;
}
},
| FnArg::Typed(PatType { pat, .. }) => {
*pat = parse_quote!( _ );
},
});
quote!(
#pub_
#default_
#sig;
)
},
| ImplItem::Type(ImplItemType {
vis: pub_,
defaultness: default_,
type_token: type_,
ident: TypeName @ _,
generics,
semi_token: SEMICOLON @ _,
..
}) => quote! (
#pub_
#default_
#type_ #TypeName #generics
:
?::ext_trait::__::core::marker::Sized
#SEMICOLON
),
| _ => return Err(Error::new_spanned(it, "unsupported `impl` entry")),
})).collect::<Result<Vec<_>>>()?;
Ok(quote_spanned!(trait_def_span=>
#[allow(nonstandard_style)]
#pub_
#trait_ #TraitName #intro_generics
#where_clause
{
#(#each_entry)*
}
#item_impl
))
}