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
#[proc_macro_derive(Deref, attributes(deref))]
pub fn derive_deref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);
let ident = input.ident;
let target = DerefTarget::get(&input.data);
let target_ty = target.ty;
let target_field = target.field;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let expanded = quote::quote! {
#[automatically_derived]
impl #impl_generics ::std::ops::Deref for #ident #ty_generics #where_clause {
type Target = #target_ty;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.#target_field
}
}
};
proc_macro::TokenStream::from(expanded)
}
#[proc_macro_derive(DerefMut, attributes(deref))]
pub fn derive_deref_mut(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);
let ident = input.ident;
let target = DerefTarget::get(&input.data);
let target_field = target.field;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let expanded = quote::quote! {
#[automatically_derived]
impl #impl_generics ::std::ops::DerefMut for #ident #ty_generics #where_clause {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.#target_field
}
}
};
proc_macro::TokenStream::from(expanded)
}
struct DerefTarget {
ty: syn::Type,
field: proc_macro2::TokenStream,
has_attr: bool,
}
impl DerefTarget {
const ATTR_NAME: &'static str = "deref";
fn has_attr(attrs: &[syn::Attribute]) -> bool {
attrs.iter().any(|attr| attr.path.is_ident(Self::ATTR_NAME))
}
fn get_target(mut targets: impl ExactSizeIterator<Item = Self>) -> Self {
if targets.len() == 1 {
targets.next().unwrap()
} else {
let targets = targets.filter(|target| target.has_attr).collect::<Vec<_>>();
if targets.len() == 1 {
targets.into_iter().next().unwrap()
} else {
panic!("expected exactly one field with #[deref] attribute");
}
}
}
fn get(data: &syn::Data) -> Self {
match data {
syn::Data::Struct(data) => match data.fields {
syn::Fields::Named(ref fields) => {
let fields = fields.named.iter().map(|f| {
let ty = f.ty.clone();
let field = f.ident.clone().unwrap();
let has_attr = Self::has_attr(&f.attrs);
Self {
ty,
field: quote::quote!(#field),
has_attr,
}
});
Self::get_target(fields)
}
syn::Fields::Unnamed(ref fields) => {
let fields = fields.unnamed.iter().enumerate().map(|(i, f)| {
let ty = f.ty.clone();
let field = syn::Index::from(i);
let has_attr = Self::has_attr(&f.attrs);
Self {
ty,
field: quote::quote!(#field),
has_attr,
}
});
Self::get_target(fields)
}
syn::Fields::Unit => {
panic!("cannot be derived for unit structs")
}
},
_ => unimplemented!("can only be derived for structs"),
}
}
}