@@ -206,262 +206,3 @@ got:
206
206
}
207
207
} ;
208
208
}
209
-
210
- /// A helper macro for declaring relatively straightforward derive
211
- /// implementations. It provides mechanisms for operating over structures
212
- /// performing modifications on each field etc.
213
- ///
214
- /// This macro doesn't define the actual derive, but rather the implementation
215
- /// method. Use `decl_derive!` to generate the implementation wrapper.
216
- ///
217
- /// # Stability Warning
218
- ///
219
- /// This is an unstable experimental macro API, which may be changed or removed
220
- /// in a future version. I'm not yet confident enough that this API is useful
221
- /// enough to warrant its complexity and inclusion in `synstructure`.
222
- ///
223
- /// # Caveat
224
- ///
225
- /// The `quote!` macro from `quote` must be imported in the calling crate, as
226
- /// this macro depends on it.
227
- ///
228
- /// # Note
229
- ///
230
- /// This feature is implemented behind the `simple-derive` feature, and is only
231
- /// available when that feature is enabled.
232
- ///
233
- /// # Example
234
- ///
235
- /// ```
236
- /// # use quote::quote;
237
- /// # const _IGNORE: &'static str = stringify! {
238
- /// synstructure::decl_derive!([Interest] => derive_interest);
239
- /// # };
240
- ///
241
- /// synstructure::simple_derive! {
242
- /// // This macro implements the `Interesting` method exported by the `aa`
243
- /// // crate. It will explicitly add an `extern crate` invocation to import the
244
- /// // crate into the expanded context.
245
- /// derive_interest impl synstructure_test_traits::Interest {
246
- /// // A "filter" block can be added. It evaluates its body with the (s)
247
- /// // variable bound to a mutable reference to the input `Structure`
248
- /// // object.
249
- /// //
250
- /// // This block can be used to perform general transformations, such as
251
- /// // filtering out fields which should be ignored by all methods and for
252
- /// // the purposes of binding type parameters.
253
- /// filter(s) {
254
- /// s.filter(|bi| bi.ast().ident != Some(syn::Ident::new("a",
255
- /// proc_macro2::Span::call_site())));
256
- /// }
257
- ///
258
- /// // This is an implementation of a method in the implemented crate. The
259
- /// // return value should be the series of match patterns to destructure
260
- /// // the `self` argument with.
261
- /// fn interesting(&self as s) -> bool {
262
- /// s.fold(false, |acc, bi| {
263
- /// quote!(#acc || synstructure_test_traits::Interest::interesting(#bi))
264
- /// })
265
- /// }
266
- /// }
267
- /// }
268
- ///
269
- /// fn main() {
270
- /// synstructure::test_derive!{
271
- /// derive_interest {
272
- /// struct A<T> {
273
- /// x: i32,
274
- /// a: bool, // Will be ignored by filter
275
- /// c: T,
276
- /// }
277
- /// }
278
- /// expands to {
279
- /// #[allow(non_upper_case_globals)]
280
- /// #[doc(hidden)]
281
- /// const _DERIVE_synstructure_test_traits_Interest_FOR_A: () = {
282
- /// extern crate synstructure_test_traits;
283
- /// impl<T> synstructure_test_traits::Interest for A<T>
284
- /// where T: synstructure_test_traits::Interest
285
- /// {
286
- /// fn interesting(&self) -> bool {
287
- /// match *self {
288
- /// A {
289
- /// x: ref __binding_0,
290
- /// c: ref __binding_2,
291
- /// ..
292
- /// } => {
293
- /// false ||
294
- /// synstructure_test_traits::Interest::interesting(__binding_0) ||
295
- /// synstructure_test_traits::Interest::interesting(__binding_2)
296
- /// }
297
- /// }
298
- /// }
299
- /// }
300
- /// };
301
- /// }
302
- /// }
303
- /// }
304
- /// ```
305
- #[ cfg( feature = "simple-derive" ) ]
306
- #[ macro_export]
307
- macro_rules! simple_derive {
308
- // entry point
309
- (
310
- $iname: ident impl $path: path { $( $rest: tt) * }
311
- ) => {
312
- $crate:: simple_derive!( __I [ $iname, $path] { $( $rest) * } [ ] [ ] ) ;
313
- } ;
314
-
315
- // Adding a filter block
316
- (
317
- __I $opt: tt {
318
- filter( $s: ident) {
319
- $( $body: tt) *
320
- }
321
- $( $rest: tt) *
322
- } [ $( $done: tt) * ] [ $( $filter: tt) * ]
323
- ) => {
324
- $crate:: simple_derive!(
325
- __I $opt { $( $rest) * } [ $( $done) * ] [
326
- $( $filter) *
327
- [
328
- st_name = $s,
329
- body = {
330
- $( $body) *
331
- } ,
332
- ]
333
- ]
334
- ) ;
335
- } ;
336
-
337
- // &self bound method
338
- (
339
- __I $opt: tt {
340
- fn $fn_name: ident ( & self as $s: ident $( $params: tt) * ) $( -> $t: ty) * {
341
- $( $body: tt) *
342
- }
343
- $( $rest: tt) *
344
- } [ $( $done: tt) * ] [ $( $filter: tt) * ]
345
- ) => {
346
- $crate:: simple_derive!(
347
- __I $opt { $( $rest) * } [
348
- $( $done) *
349
- [
350
- st_name = $s,
351
- bind_style = Ref ,
352
- body = { $( $body) * } ,
353
- result = result,
354
- expanded = {
355
- fn $fn_name( & self $( $params) * ) $( -> $t) * {
356
- match * self { #result }
357
- }
358
- } ,
359
- ]
360
- ] [ $( $filter) * ]
361
- ) ;
362
- } ;
363
-
364
- // &mut self bound method
365
- (
366
- __I $opt: tt {
367
- fn $fn_name: ident ( & mut self as $s: ident $( $params: tt) * ) $( -> $t: ty) * {
368
- $( $body: tt) *
369
- }
370
- $( $rest: tt) *
371
- } [ $( $done: tt) * ] [ $( $filter: tt) * ]
372
- ) => {
373
- $crate:: simple_derive!(
374
- __I $opt { $( $rest) * } [
375
- $( $done) *
376
- [
377
- st_name = $s,
378
- bind_style = RefMut ,
379
- body = { $( $body) * } ,
380
- result = result,
381
- expanded = {
382
- fn $fn_name( & mut self $( $params) * ) $( -> $t) * {
383
- match * self { #result }
384
- }
385
- } ,
386
- ]
387
- ] [ $( $filter) * ]
388
- ) ;
389
- } ;
390
-
391
- // self bound method
392
- (
393
- __I $opt: tt {
394
- fn $fn_name: ident ( self as $s: ident $( $params: tt) * ) $( -> $t: ty) * {
395
- $( $body: tt) *
396
- }
397
- $( $rest: tt) *
398
- } [ $( $done: tt) * ] [ $( $filter: tt) * ]
399
- ) => {
400
- $crate:: simple_derive!(
401
- __I $opt { $( $rest) * } [
402
- $( $done) *
403
- [
404
- st_name = $s,
405
- bind_style = Move ,
406
- body = { $( $body) * } ,
407
- result = result,
408
- expanded = {
409
- fn $fn_name( self $( $params) * ) $( -> $t) * {
410
- match self { #result }
411
- }
412
- } ,
413
- ]
414
- ] [ $( $filter) * ]
415
- ) ;
416
- } ;
417
-
418
- // XXX: Static methods?
419
-
420
- // codegen after data collection
421
- (
422
- __I [ $iname: ident, $path: path] { } [ $(
423
- [
424
- st_name = $st_name: ident,
425
- bind_style = $bind_style: ident,
426
- body = $body: tt,
427
- result = $result: ident,
428
- expanded = { $( $expanded: tt) * } ,
429
- ]
430
- ) * ] [ $(
431
- [
432
- st_name = $filter_st_name: ident,
433
- body = $filter_body: tt,
434
- ]
435
- ) * ]
436
- ) => {
437
- fn $iname( mut st: $crate:: Structure ) -> $crate:: macros:: TokenStream2 {
438
- let _ = & mut st; // Silence the unused mut warning
439
-
440
- // Filter/transform the `Structure` object before cloning it for
441
- // individual methods.
442
- $(
443
- {
444
- let $filter_st_name = & mut st;
445
- $filter_body
446
- }
447
- ) *
448
-
449
- // Clone the `Structure` object and set the correct binding style,
450
- // then perform method specific expansion.
451
- $(
452
- let $result = {
453
- let mut $st_name = st. clone( ) ;
454
- $st_name. bind_with( |_| $crate:: BindStyle :: $bind_style) ;
455
- let $result = {
456
- $body
457
- } ;
458
- $crate:: quote!{ $( $expanded) * }
459
- } ;
460
- ) *
461
-
462
- st. bound_impl( $crate:: quote!( $path) , $crate:: quote!{
463
- $( #$result) *
464
- } )
465
- }
466
- }
467
- }
0 commit comments