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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#![recursion_limit = "256"]
extern crate proc_macro;

mod attrib;
mod intrinsic;

use proc_macro2::{Span, TokenStream};
use quote::{quote, TokenStreamExt};
use syn::{
    Data, DataStruct, DeriveInput, Expr, Fields, GenericArgument, Generics, Ident, Index, Path,
    PathArguments, Type, TypeArray, TypePath,
};

/// Intrinsic derive macro. Intrinsic is not a real trait, but an indicator that intrinsic
/// intrinsic traits need to be implemented for this type. The name of the trait itself is
/// specified by the intrinsic attribute, which is to be specified in front of a field containing
/// the `IntrinsicAttribute` type.
#[proc_macro_derive(Intrinsic, attributes(intrinsics, intrinsic))]
pub fn intrinsic(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input: DeriveInput = syn::parse(input).unwrap();

    let gen = intrinsic::impl_intrinsic(&input);

    gen.into()
}

/// Derive macro for implementing the `Attrib` trait for given struct.
#[proc_macro_derive(Attrib, attributes(attributes))]
pub fn attrib(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input: DeriveInput = syn::parse(input).unwrap();

    let gen = attrib::impl_attrib(&input);

    gen.into()
}

#[proc_macro_derive(NewCollectionType)]
pub fn new_collection_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input: DeriveInput = syn::parse(input).unwrap();

    let gen = impl_new_collection_type(&input);

    gen.into()
}

fn impl_new_array_collection(
    generics: &Generics,
    name: &Ident,
    parm: &Type,
    len: &Expr,
    field_idx: Index,
) -> TokenStream {
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    quote! {
        //impl #impl_generics ::std::ops::Deref for #name #ty_generics #where_clause {
        //    type Target = [#parm;#len];
        //    #[inline]
        //    fn deref(&self) -> &[#parm;#len] {
        //        &self.#field_idx
        //    }
        //}
        //impl #impl_generics ::std::ops::DerefMut for #name #ty_generics #where_clause {
        //    #[inline]
        //    fn deref_mut(&mut self) -> &mut [#parm;#len] {
        //        &mut self.#field_idx
        //    }
        //}
        impl #impl_generics #name #ty_generics #where_clause {
            /// Get the underlying container.
            #[inline]
            pub fn get(&self) -> &[#parm;#len] {
                &self.#field_idx
            }
            #[inline]
            pub fn get_mut(&mut self) -> &mut [#parm;#len] {
                &mut self.#field_idx
            }
            #[inline]
            pub fn into_inner(self) -> [#parm;#len] {
                self.0
            }
        }

        impl #impl_generics Into<[#parm;#len]>for #name #ty_generics #where_clause {
            #[inline]
            fn into(self) -> [#parm;#len] {
                self.0
            }
        }

        impl #impl_generics ::std::ops::Index<usize>for #name #ty_generics #where_clause {
            type Output = #parm;
            #[inline]
            fn index(&self, index: usize) -> &#parm {
                &self.#field_idx[index]
            }
        }
        impl #impl_generics ::std::ops::IndexMut<usize>for #name #ty_generics #where_clause {
            #[inline]
            fn index_mut(&mut self, index: usize) -> &mut #parm {
                &mut self.#field_idx[index]
            }
        }
    }
}

fn impl_new_vec_collection(
    generics: &Generics,
    name: &Ident,
    vec_type: &Type,
    parm: &GenericArgument,
    field_idx: Index,
) -> TokenStream {
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    quote! {
        //impl #impl_generics ::std::ops::Deref for #name #ty_generics #where_clause {
        //    type Target = #vec_type;
        //    #[inline]
        //    fn deref(&self) -> &#vec_type {
        //        &self.#field_idx
        //    }
        //}
        //impl #impl_generics ::std::ops::DerefMut for #name #ty_generics #where_clause {
        //    #[inline]
        //    fn deref_mut(&mut self) -> &mut #vec_type {
        //        &mut self.#field_idx
        //    }
        //}
        impl #impl_generics #name #ty_generics #where_clause {
            /// Get the underlying container.
            #[inline]
            pub fn get(&self) -> &#vec_type {
                &self.#field_idx
            }
            #[inline]
            pub fn get_mut(&mut self) -> &mut #vec_type {
                &mut self.#field_idx
            }
            #[inline]
            pub fn capacity(&self) -> usize {
                self.#field_idx.capacity()
            }
            #[inline]
            pub fn reserve(&mut self, additional: usize) {
                self.#field_idx.reserve(additional)
            }
            #[inline]
            pub fn reserve_exact(&mut self, additional: usize) {
                self.#field_idx.reserve_exact(additional)
            }
            #[inline]
            pub fn shrink_to_fit(&mut self) {
                self.#field_idx.shrink_to_fit()
            }
            #[inline]
            pub fn into_boxed_slice(self) -> Box<[#parm]>{
                self.#field_idx.into_boxed_slice()
            }
            #[inline]
            pub fn truncate(&mut self, len: usize) {
                self.#field_idx.truncate(len)
            }
            #[inline]
            pub fn as_slice(&self) -> &[#parm] {
                self.#field_idx.as_slice()
            }
            #[inline]
            pub fn as_mut_slice(&mut self) -> &mut [#parm] {
                self.#field_idx.as_mut_slice()
            }
            // unsafe function set_len skipped
            #[inline]
            pub fn swap_remove(&mut self, index: usize) -> #parm {
                self.#field_idx.swap_remove(index)
            }
            #[inline]
            pub fn insert(&mut self, index: usize, element: #parm) {
                self.#field_idx.insert(index, element)
            }
            #[inline]
            pub fn remove(&mut self, index: usize) -> #parm {
                self.#field_idx.remove(index)
            }
            #[inline]
            pub fn retain<F>(&mut self, f: F) where F: FnMut(&#parm) -> bool {
                self.#field_idx.retain(f)
            }
            #[inline]
            pub fn push(&mut self, value: #parm) {
                self.#field_idx.push(value)
            }
            #[inline]
            pub fn pop(&mut self) -> Option<#parm> {
                self.#field_idx.pop()
            }
            #[inline]
            pub fn append(&mut self, other: &mut Vec<#parm>) {
                self.#field_idx.append(other)
            }
            // Skipped drain due to unstable API
            #[inline]
            pub fn clear(&mut self) {
                self.#field_idx.clear()
            }
            #[inline]
            pub fn len(&self) -> usize {
                self.#field_idx.len()
            }
            #[inline]
            pub fn is_empty(&self) -> bool {
                self.#field_idx.is_empty()
            }
            #[inline]
            pub fn split_off(&mut self, at: usize) -> Vec<#parm> {
                self.#field_idx.split_off(at)
            }
        }

        impl #impl_generics Into<#vec_type>for #name #ty_generics #where_clause {
            #[inline]
            fn into(self) -> #vec_type {
                self.0
            }
        }

        impl #impl_generics ::std::ops::Index<usize>for #name #ty_generics #where_clause {
            type Output = #parm;
            #[inline]
            fn index(&self, index: usize) -> &#parm {
                &self.#field_idx[index]
            }
        }
        impl #impl_generics ::std::ops::IndexMut<usize>for #name #ty_generics #where_clause {
            #[inline]
            fn index_mut(&mut self, index: usize) -> &mut #parm {
                &mut self.#field_idx[index]
            }
        }
    }
}

fn impl_new_collection_type(ast: &DeriveInput) -> TokenStream {
    let mut tokens = TokenStream::new();
    let name = &ast.ident;

    let supported_collection_types = vec!["Vec"];

    if let Data::Struct(DataStruct {
        fields: Fields::Unnamed(ref fields),
        ..
    }) = ast.data
    {
        for (i, field) in fields.unnamed.iter().enumerate() {
            if let Type::Path(TypePath {
                path: Path { ref segments, .. },
                ..
            }) = field.ty
            {
                if segments.last().is_none() {
                    continue;
                }

                let ty = segments.last().unwrap();

                if supported_collection_types
                    .iter()
                    .find(|&&x| Ident::new(x, Span::call_site()) == ty.ident)
                    .is_none()
                {
                    continue;
                }

                if let PathArguments::AngleBracketed(ref angled_args) = ty.arguments {
                    if angled_args.args.len() != 1 {
                        panic!(
                            "New collection type must wrap a collection that contains exactly\
                             one type."
                        );
                    }

                    let parm = angled_args.args.first().unwrap();

                    let field_idx = Index::from(i);
                    tokens.append_all(impl_new_vec_collection(
                        &ast.generics,
                        name,
                        &field.ty,
                        parm,
                        field_idx,
                    ));
                } else {
                    panic!("New collection type must wrap a collection that contains a type.");
                }
            } else if let Type::Array(TypeArray {
                ref elem, ref len, ..
            }) = field.ty
            {
                let parm = &**elem;

                let field_idx = Index::from(i);

                tokens.append_all(impl_new_array_collection(
                    &ast.generics,
                    name,
                    parm,
                    len,
                    field_idx,
                ));
            } else {
                panic!("New collection type must wrap a Vec or an array.");
            }
        }
    }
    tokens
}