newtypedecl 0.0.1

declarative newtype macro
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907




struct  NewtypeDefinition{
    attrs: Vec<Attribute>,
    vis: Visibility,
    struct_token: Struct,
    ident: Ident,
    generics: Generics,
    fields: Punctuated<Field, Comma>,
    where_clause: Option<WhereClause>,
    control_block: Option<Punctuated<ControlDef, Semi>>,
}

enum ControlDef{
    FunctionDef(FunctionDefinition),
    TraitDef(TraitDefinition),
}

enum FunctionDefinition{
    Suppressed {
        not: Token![!],
        fn_token: Fn,
        ident: Ident,
    },
    Automatic {
        fn_token: Fn,
        ident: Ident,
    },
    //not: Token![!],
    //fn_token: Fn,
    //ident: Ident,
    // generics: Generics,
    // paren_token: Paren,
    // inputs: Punctuated<FnArg, Comma>,
    // output: ReturnType,
    // where_clause: Option<WhereClause>,
    // control_block: Option<Punctuated<Item, Semi>>,
}


enum TraitDefinition{
    Suppressed {
        not: Token![!],
        trait_token: Trait,
         ident: Ident,
    },
    Automatic {
        trait_token: Trait,
        ident: Ident,
    },
    Manual {
        trait_token: Trait,
        ident: Ident,
        // impl block
    },
}


struct NewtypeDefinitions{
    definitions: Punctuated<NewtypeDefinition, Semi>,
}




impl Parse for NewtypeDefinition {
    fn parse(input: ParseStream) -> Result<Self> {

        let fields;
        let control_block;

        Ok(
            NewtypeDefinition {
                attrs: input.call(Attribute::parse_outer)?,
                vis: input.parse()?,
                struct_token: input.parse()?,
                ident: input.parse()?,
                generics: input.parse()?,
                fields: {parenthesized!(fields in input); fields.parse_terminated(Field::parse_unnamed, Comma)?},
                where_clause: input.parse()?,
                control_block: {parenthesized!(control_block in input); control_block.parse_terminated(Item::parse, Semi)?},
            }

        )
        // let attributes = input.call(Attribute::parse_outer)?;
        // let vis: Visibility = input.parse()?;
        // 
        // input.parse::<Token![struct]>()?;
        // 
        // let name = input.parse::<Ident>()?;
        // let generics = input.parse::<syn::Generics>()?;
        // 
        // let fields;        
        // parenthesized!(fields in input);
        // let fields: Punctuated<Field, Token![,]> = fields.parse_terminated(Field::parse_unnamed, Token![,])?;
        //     //.parse_terminated(Field::parse_unnamed)?;
        // //= input.call(syn::Field::parse_unnamed)?;
        // 
        // let where_clause= input.parse::<syn::WhereClause>().ok();

        //let fields;        
        //parenthesized!(fields in input);
        //let fields: Punctuated<Field, Token![,]> = fields.parse_terminated(Field::parse_unnamed, Token![,])?;
        
        
        // input.parse::<Token![;]>()?;
        // 
        // 
        // Ok(
        //     NewtypeDefinition{
        //         //attributes,
        //         //vis,
        //     }
        // )
        
        
//        let fields = input.parse::<syn::Fields>()?;

  //        let attribute = input.parse::<syn::Attribute>();


        
        // let lookahead = input.lookahead1();
        // 
        // 
        // let attributes: Vec<syn::Attribute> = Vec::new();
        // 
        // 
        // // parse attributes
        // while lookahead.peek(Token![#]) {
        //     
        //     input.parse::<syn::Attribute>().map(|attr| attributes.push(attr));
        //     
        // } //else
            //if lookahead.peek(Token![enum]) {
        //     input.parse().map(Item::Enum)
        // } else {
        //     Err(lookahead.error())
        // }

//        todo!()
           
    }
}


#[proc_macro]
pub fn newtype(tt: TokenStream) -> TokenStream {
    let ttt = tt.clone();
    let newtype= parse_macro_input!(tt as NewtypeDefinition);
    //let newtype= parse_macro_input!(tt as DeriveInput);
    //let attributes = Vec::new();


    
    //let ii= tt.into_iter();
    // 
    // 
    //eprintln!("{newtype:#?}");
    // eprintln!("{tt2:#?}");
//    eprintln!("{tt}");
    //"fn answer() -> u32 { 42 }".parse().unwrap()
    //tt.into()

    ttt //todo!()
}



// PLANNED: 'where' clauses


// // macro macro generation at toplevel
// 
// /// Checks if one of the given patterns appears in a list, and returns either the `then` or
// /// `else` part.
// #[doc(hidden)]
// #[macro_export]
// macro_rules! contains {
//     (dollar_sign ($d:tt) if ($(($($pattern:tt)*))+) in defs ($($list:tt)*) $(then ($($yes:tt)*))? $(else ($($no:tt)*))?) => (
//         $crate::contains!(dollar_sign ($) if ($(($($pattern)*))+) in ($crate::defs_to_list($($list)*))
//                           $(then ($($yes)*))? $(else ($($no)*))?);
//     );
// 
//     (dollar_sign ($d:tt) if ($(($($pattern:tt)*))+) in ($(($($list:tt)*))*) $(then ($($yes:tt)*))? $(else ($($no:tt)*))?) => (
//         macro_rules! contains_intern {
//             // generate one clause for each pattern
//             $((@rec ($($pattern)*)$d(($_:tt))*) => (contains_intern!(@yes));)+
//             // no match recursion step
//             (@rec ($_:tt)$d(($tail:tt))*) => (contains_intern!(@rec $d(($tail))*));
// 
//             // end of recursion, nothing found
//             (@rec) => ($($($no)*)?);
// 
//             // error handling
//             (@rec $d($error:tt)*) => (compile_error!(stringify!(Error in newtype list < $d($error)* >)));
// 
//             // branch when something was found
//             (@yes) => ($($($yes)*)?);
//         }
//         contains_intern!(@rec $(($($list)*))*);
//     );
// 
//     // error handling, be nice and give a hint
//     (dollar_sign ($d:tt) if () $($_:tt)*) => (compile_error!(stringify!(Syntax error: if needs a non empty list)));
// 
//     // entry point
//     (if $($rest:tt)*) => (
//         // need to pass $ because of https://github.com/rust-lang/rust/issues/35853
//         $crate::contains!(dollar_sign ($) if $($rest)*);
//     );
// 
//     // error handling
//     (&($error:tt)*) => (compile_error!(stringify!(Syntax error in contains! macro < $d($error)* >)));
// }
// 
// #[test]
// fn contains_macro() {
//     contains!(if ((foo bar)) in () then (panic!()) else ());
//     contains!(if ((foo)) in ((foo)(bar)) then (println!("ok")) else (panic!()));
//     contains!(if ((foo)) in ((foo)(bar)(baz)) then (println!("ok")) else (panic!()));
//     contains!(if ((bar)) in ((foo)(bar)(baz)) then (println!("ok")) else (panic!()));
//     contains!(if ((baz)) in ((foo)(bar)(baz)) then (println!("ok")) else (panic!()));
//     contains!(if ((barf)) in ((foo)(bar)(baz)) then (panic!()) else (println!("ok")));
//     contains!(if ((barf)(bar)) in ((foo)(bar)(baz)) then (println!("ok")) else (panic!()));
//     contains!(if ((foo)(bar)) in ((foo)(bar)(baz)) then (println!("ok")) else (panic!()));
// 
//     // then and else are optional
//     contains!(if ((foo)) in ((foo)(bar)));
//     contains!(if ((foo)) in ((foo)(bar)) then (println!("ok")));
//     contains!(if ((barf)) in ((foo)(bar)(baz)) else (println!("ok")));
// }
// 
// /// Extracts 'fn name', 'trait name' and the negations, return them as list
// /// usable with the `contains!()` macro.
// #[doc(hidden)]
// #[macro_export]
// macro_rules! defs_to_list {
//     // matching all function defs
//     (!fn $name:ident;$($tail:tt)*) => (
//         //show!(!fn $name);
//         (!fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($pub:vis fn $name:ident$(<$($generics:tt),*>)?$(($($params:tt)*) $(-> $ret:ty)?)?; $($tail:tt)*) => (
//         //show!(fn $name);
//         (fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($pub:vis fn $name:ident$(<$($generics:tt),*>)?$(($($params:tt)*) $(-> $ret:ty)?)?$block:block $($tail:tt)*) => (
//         //show!(fn $name);
//         (fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($pub:vis const fn $name:ident$(<$($generics:tt),*>)?$(($($params:tt)*) $(-> $ret:ty)?)?; $($tail:tt)*) => (
//         //show!(fn $name);
//         (fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($pub:vis const fn $name:ident$(<$($generics:tt),*>)?$(($($params:tt)*) $(-> $ret:ty)?)?$block:block $($tail:tt)*) => (
//         //show!(fn $name);
//         (fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
// 
//     // matching all trait defs
//     (!trait $name:ident;$($tail:tt)*) => (
//         //show!(!trait $name);
//         (!trait $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     (trait$(<$($generics1:tt),*>)? $name:ident$(<$($generics2:tt),*>)?;$($tail:tt)*) => (
//         //show!(trait $name);
//         (trait $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     (trait$(<$($generics1:tt),*>)? $name:ident$(<$($generics2:tt),*>)?$block:block $($tail:tt)*) => (
//         //show!(trait $name);
//         (trait $name)
//         $crate::defs_to_list!($($tail)*)
//     );
// 
//     // end of recursion, excess semicolon or error
//     () => ();
//     (; $($tail:tt)*) => (
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($($error:tt)*) => (compile_error!(stringify!(Error in newtype "defs" block at < $($error)* > )));
// }
// 
// // This works only when the '//show!(..' lines are uncommented and the real output is commented
// // #[test]
// // fn defs_to_list_test() {
// //     defs_to_list! {
// //         !fn baz;
// //         fn foo;
// //         fn bar {}
// //         pub const fn pfn { 42 }
// //         fn more();
// //         fn more1(&self);
// //         fn more2() -> i32;
// //         fn more3() -> i32 {42}
// //         fn generic<'a,T>(&self, t:T) -> U;
// //         !trait Bar;
// //         trait Foo;
// //         trait Baz { fn baz(){} }
// //         trait<'a, T> Baz<'a,T>;
// //         trait<'a, T> Baz<'a,T> { fn baz(){} };;
// //        };
// // }

// /// The newtype macro creates a struct with the given name and generates the boilerplate code
// /// that comes with the newtype idiom.
// ///
// /// # Syntax
// ///
// /// This gives a rough idea of the syntax used. For more details, see the below.  Many parts
// /// are optional and can be left out. In simple words this is a usual newtype definition
// /// followed by a block of abbreviated function and trait definitions in curly braces.
// ///
// /// ```rust,ignore
// /// newtype!(
// ///     // Simple newtype
// ///     struct SimpleNewTypeName(WrappedType);
// ///
// ///     // Complex newtype
// ///     /// doc comment
// ///     #[attributes]
// ///     pub struct NewTypeName<'lifetimes, generics>(pub WrappedType, MarkerTypes) {
// ///         // automatic trait implementation
// ///         trait Trait;
// ///         // explicit trait implementation
// ///         trait Trait { fn traitfn(){} }
// ///         // automatic function implementation on `impl NewTypeName { ...`
// ///         const fn name(params) -> ReturnType;
// ///         fn name(params) -> ReturnType;
// ///         // explicit function implementation
// ///         fn name(params) -> ReturnType { todo!() };
// /// TODO: method forwarding fn foo(&self,$(params)) -> DerefReturn; { self.0.foo($(params)) }
// ///         // suppress predefined trait and function implementations
// ///         !trait Trait;
// ///         !fn name;
// ///     }
// /// );
// /// ```
// ///
// /// # The newtype macro generates
// ///
// /// ## The struct
// ///
// /// The struct is generated as given. The newtype idiom is represented by a struct with only a
// /// single data member of the wrapped type. We expect this to be a tuple struct. This tuple
// /// can have additional zero length marker entries such as `PhantomData`. Lifetimes and
// /// generics can be used as usual. One can define any number of attributes before the actual
// /// struct definition. This includes rustdoc comments as these are just syntactic sugar for
// /// attributes. Both, the struct and the wrapped type can be declared `pub`. Newtype structs
// /// will always be `#[repr(transparent)]`, this ensures that one can't pass non ZST types as
// /// markers and that the memory layout always matches the wrapped type.
// // PLANNED: are there cases where we need to suppress this? -> #[repr(Rust)] or any other prevents this
// ///
// /// ## The structs `impl` block
// ///
// /// `newtype` generates an impl block for the struct, containing the functions below.
// /// When this is not enough one can add additional impl blocks after the newtype macro.
// ///
// /// ### `fn new(t: WrappedType) -> Self`
// ///
// /// The constructor for creating this struct. This function will be `pub` when the struct is `pub`.
// /// One can override this with a custom implementation eg. `const fn new(t: Into<T>) -> Self;`
// ///
// /// The override can have none or one parameter. When only the prototype is given then the
// /// code is generated automatically. In case of zero arguments a default constructed value is
// /// returned. When one argument is given then this will be used to construct the value via
// /// `.into()`. In case the implementation block is given the number of parameters are not
// /// limited.
// ///
// /// ### `fn take(s: Self) -> WrappedType`
// ///
// /// This associated function takes the value out of the newtype. This function will be `pub`
// /// when the struct is `pub`. The generation of this function can be suppressed with `!fn
// /// take;`.
// ///
// /// ### `fn get(&self) -> &WrappedType` and `fn get_mut(&mut self) -> &mut WrappedType`
// ///
// /// These methods are not automatically generated. To get these specify `fn get;` and `fn
// /// get_mut;`.
// ///
// /// ## Automatic Traits
// ///
// /// The following traits are by default implemented:
// ///
// /// * `AsRef<WrappedType>` and `AsMut<WrappedType>`
// /// * `Borrow<WrappedType>` and `BorrowMut<WrappedType>`
// /// * `From<WrappedType>`
// ///
// /// Either of these can be suppressed with specifying `!trait Trait;`.
// ///
// /// `Deref` and `DerefMut` are not implemented by default. This can be done by specifying `trait
// /// Deref;` or `trait DerefMut;`. They dereference to the wrapped type. When something else is
// /// desired then they can be explicitly defined.
// ///
// /// ## Manual Traits
// ///
// /// By writing `trait Trait { fn traitfn(){} }` one can implement any trait for the newtype.
// /// The `for NewType` part and lifetimes are inserted automatically when not specified.
// /// It is possible to implement traits outside of the newtype macro as well.
// #[macro_export]
// macro_rules! newtype {
//     (
//         $(
//             $(#[$($attrs:meta),*])*
//                 $pub:vis struct
//                 $name:ident$(<$($generics:tt),*>)? (
//                     $pub_inner:vis
//                         $type:ty
//                         $(, $($markers:ident)::* $(<$($markers_generics:tt),*>)?)*
//                 )
//                 $({$($defs:tt)*})?
//         );*
//     ) => {
//         $(
//             #[repr(transparent)]
//             $($(#[$attrs])*)*
//             $pub struct $name$(<$($generics),*>)?($pub_inner $type $(, $($markers)::* $(<$($markers_generics),*>)? )*);
// 
// 
//             //$crate::contains!{if ((fn new)(!fn new)) in ($crate::defs_to_list!($($defs)*))}
//             $(
//                 //$crate::contains!{if ((fn new)(!fn new)) in ($crate::defs_to_list!($($defs),*))}
//                 $crate::contains!{if ((fn new)(!fn new)) in defs($($defs)*)}
//             )?
// 
//             $($crate::defs_to_list!($($defs)*);)?
//           // $crate::contains!{if ((fn new)(!fn new)) in ($crate::defs_to_list!($($defs)*)) then (
//           //     // impl$(<$($generics),*>)? $name$(<$($generics),*>)? {
//           //     // }
//           // )}
// 
// 
//         )*
//     };
// }


          
          // // since macros cant be defined in impl blocks or traits we need to do this here and expand right away
          // macro_rules! newtype_impls {
          //     ($d:tt) => {
          // 
          // 
          //         impl$(<$($generics),*>)? $name$(<$($generics),*>)? {
          //             // PLANNED: needs explicit prototype or implementation
          // 
          // 
          //             
          //             // if () in () then (
          //             //     ;
          //             //
          //             //
          //             //
          //             //
          //             //     //     $crate:newtype_fn!([$type $(, $($markers)::*)*] $pub fn new);
          //             //     // $pub fn new(value: $type) -> Self {
          //             //     //     Self(value $(, $($markers)::* )*)
          //             //     // }
          //             //
          //             //                )
          // 
          //             /// Takes the value out of the newtype.
          //             // PLANNED: !fn take   @ifnot explicit
          //             $pub fn take(value: Self) -> $type {
          //                 value.0
          //             }
          // 
          //             // $pub fn new_old(value: $type) -> Self {
          //             //     Self(value $(, $($markers)::* )*)
          //             // }
          // 
          //             // impl fns from $defs
          // 
          //         }
          // 
          //     };
          // }
          // 
          // // do the dollar_sign dance again
          // newtype_impls!($);


                  // // PLANNED: !impl AsRef  AsMut ... @ifnot explicit
                  // impl$d(<$d($generics),*>)? std::convert::AsRef<$type> for $name$d(<$d($generics),*>)? {
                  //     fn as_ref(&self) -> &$type {
                  //         &self.0
                  //     }
                  // }
                  // 

                  // impl$d(<$d($generics),*>)? std::convert::AsMut<$type> for $name$d(<$d($generics),*>)? {
                  //     fn as_mut(&mut self) -> &mut $type {
                  //         &mut self.0
                  //     }
                  // }
                  // 
                  // impl$d(<$d($generics),*>)? std::borrow::Borrow<$type> for $name$d(<$d($generics),*>)? {
                  //     fn borrow(&self) -> &$type {
                  //         &self.0
                  //     }
                  // }
                  // 
                  // impl$d(<$d($generics),*>)? std::borrow::BorrowMut<$type> for $name$d(<$d($generics),*>)? {
                  //     fn borrow_mut(&mut self) -> &mut $type {
                  //         &mut self.0
                  //     }
                  // }
                  // 
                  // // PLANNED: !impl From
                  // impl$d(<$d($generics),*>)? std::convert::From<$type> for $name$d(<$d($generics),*>)? {
                  //     fn from(value: $type) -> Self {
                  //         Self(value $d(, $d($markers)::* )*)
                  //     }
                  // }

//                   //($crate::contains!{if ((fn new)) in ()}
// 
// 
//           
//        )*

    

// /// The function/method generators
// #[doc(hidden)]
// #[macro_export]
// macro_rules! newtype_fn {
//     // new()
//     ([$type:ty $(, $($markers:ident)::*)*] $pub:vis const fn new) => {
//         $pub const fn new(value: $type) -> Self {
//             Self(value $(, $($markers)::* )*)
//         }
//     };
//     ([$type:ty $(, $($markers:ident)::*)*] $pub:vis fn new) => {
//         $pub fn new(value: $type) -> Self {
//             Self(value $(, $($markers)::* )*)
//         }
//     };
// 
//     // take()
// 
//     // get/get_mut
// 
//     // function forwarding
// 
//     // generic fn
// }

//
// #[doc(hidden)]
// #[macro_export]
// macro_rules! newtype_fn {
//     (@fn ) => {};
// }
//
// #[doc(hidden)]
// #[macro_export]
// macro_rules! newtype_trait {
//     (@fn ) => {};
// }

// macro_rules! test {
//
//     // (@type_path $name:ident $(:: $path:ident)* $(<$($lifetimes:lifetime),* $(,$generics:ident)*>)?) => (
//     //     $name $(:: $path)*
//     // );
//     // (@type_path $name:ident $(:: $path:ident)* $(<$($lifetimes:lifetime),* $(,$generics:ident)*>)?) => (
//     //     $name $(:: $path)*
//     // );
//     //(@type_path $path:tt $(<$($lifetimes:lifetime),* $(,$generics:ident)*>)?) => (
//     (@type_path $($path:ident)::* $(<$($lifetimes:lifetime),* $(,$generics:ident)*>)?) => (
//
//         type $($path)::* = ();
//         //type $path = ();
//     );
//
//     // ($($item:tt)*) => (
//     //     test!($(@type_path $item)*);
//     // );
// }
//
// test! {
// //    Foo
//     @type_path Foo<'a, T>
// }

// [20:43] <danieldg> cehteh: like macro_rules contains_foo { () => false; (foo, $($list:ident),*) => true; ($one:ident, $($list:ident),*) => contains_foo!($($list),*); }

// PLANNED: get() -> T; get() -> &T
// PLANNED: set(T);set( &T)
// PLANNED: swap(T)-> T; swap(&T) -> T
// PLANNED: when Copy or Clone are defined as attrs, then we can rely on that





// // Trivial newtype
// newtype!(
//     struct WrapedNumberA(i32);
// //    struct WrapedNumberB(i32);
// );
//
//
// // Trivial newtype
// newtype!(
//     #[derive(Debug)]
//     #[repr(Rust)]
//     /// A i32 wrapped in a newtype.
//     struct WrapedNumberC(i32, pub f64)
//         where i32: std::fmt::Debug
//     // {
//     //     fn foo;
//     //     trait Bar;
//     //     !fn new;
//     //     // provide a `new` function that overrides the automatic implementation
//     //     pub const fn new() -> Self {Self(String::new())}
//     //
//     //     // suppress the generation of the `take` function
//     //     !fn take;
//     //
//     //     // generates a method `push_str` that forwards to `String::push_str()`
//     //     pub fn push_str(&self, s: &str);
//     //
//     //     // implement a custom method
//     //     pub fn hello(&self) {
//     //         println!("Hello {}!", self.0);
//     //     }
//     //
//     //     // implement a trait
//     //     trait Hello {
//     //         fn hello(&self) {
//     //             println!("Hello {}!", self.0);
//     //         }
//     //     }
//     //
//     //     // suppress the automatic implementation of the `From` trait
//     //     !impl From;
//     // }
//         ;
// );

// Attribute macro syntax
// #[newtype]
// struct WrapedNumberD(i32);
// #[newtype(controlblock)]
// struct WrapedNumberD(i32);
// #[newtype{controlblock}]
// struct WrapedNumberD(i32);

// #[test]
// fn smoke() {
//     let wn = WrapedNumber(42);
//     println!("{}", wn.0);
//     assert_eq!(wn.0, 42);
// //    assert_eq!(WrapedNumber::take(wn), 42);
// }

// // With docs
// newtype!(
//     /// This is a doc comment
//     struct WithDoc(())
// );
//
// // With lifetime & attributes
// newtype!(
//     #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
//     pub struct Text<'a>(pub &'a str)
// );
//
// #[test]
// fn text() {
//     let text = Text("Hello, world!");
//     println!("{}", text.0);
// }
//
// newtype! {
//     // With lifetime & generics
//     pub struct ValueA<'a,T>(&'a T);
//     // With generics
//     pub struct ValueB<T>(T);
//     // With Marker
//     pub struct ValueC(String, std::marker::PhantomData<i32>);
// }

// // test autogenerated traits
// #[test]
// fn valueb() {
//     let valueb = ValueB::new(42);
//     assert_eq!(ValueB::take(valueb), 42);
//     println!("{}", valueb.0);
//     assert_eq!(valueb.0, 42);
// }


// macro_rules! impl_never_product_expand {
//     ($($ty:ty),*) => {
//         $(
//             impl NeedsProductExpand for $ty {
//                 fn needs_product_expand(&self) -> bool {
//                     false
//                 }
//             }
//         )*
//     };
//     (true $($ty:ty),*) => {
//         $(
//             impl NeedsProductExpand for $ty {
//                 fn needs_product_expand(&self) -> bool {
//                     false
//                 }
//             }
//         )*
//     };
//     () => {};
// }
// impl_needs_product_expand!{syn::Generics}


// // The function/method generators
// #[doc(hidden)]
// #[macro_export]
// macro_rules! newtype_fn {
//     // new()
//     ([$type:ty $(, $($markers:ident)::*)*] $pub:vis const fn new) => {
//         $pub const fn new(value: $type) -> Self {
//             Self(value $(, $($markers)::* )*)
//         }
//     };
//     ([$type:ty $(, $($markers:ident)::*)*] $pub:vis fn new) => {
//         $pub fn new(value: $type) -> Self {
//             Self(value $(, $($markers)::* )*)
//         }
//     };
// 
//     // take()
// 
//     // get/get_mut
// 
//     // function forwarding
// 
//     // generic fn
// }
// 
// 
// // macro_rules! test {
// //
// //     // (@type_path $name:ident $(:: $path:ident)* $(<$($lifetimes:lifetime),* $(,$generics:ident)*>)?) => (
// //     //     $name $(:: $path)*
// //     // );
// //     // (@type_path $name:ident $(:: $path:ident)* $(<$($lifetimes:lifetime),* $(,$generics:ident)*>)?) => (
// //     //     $name $(:: $path)*
// //     // );
// //     //(@type_path $path:tt $(<$($lifetimes:lifetime),* $(,$generics:ident)*>)?) => (
// //     (@type_path $($path:ident)::* $(<$($lifetimes:lifetime),* $(,$generics:ident)*>)?) => (
// //
// //         type $($path)::* = ();
// //         //type $path = ();
// //     );
// //
// //     // ($($item:tt)*) => (
// //     //     test!($(@type_path $item)*);
// //     // );
// // }
// //
// 
// /// Checks if one of the given patterns appears in a list, and returns either the `then` or
// /// `else` part.
// #[doc(hidden)]
// #[macro_export]
// macro_rules! contains {
//     (dollar_sign ($d:tt) if ($(($($pattern:tt)*))+) in ($(($($list:tt)*))*) $(then ($($yes:tt)*))? $(else ($($no:tt)*))?) => (
//         macro_rules! contains_intern {
//             // generate one clause for each pattern
//             $((@rec ($($pattern)*)$d(($_:tt))*) => (contains_intern!(@yes));)+
//             // no match recursion step
//             (@rec ($_:tt)$d(($tail:tt))*) => (contains_intern!(@rec $d(($tail))*));
// 
//             // end of recursion, nothing found
//             (@rec) => ($($($no)*)?);
// 
//             // error handling
//             (@rec $d($error:tt)*) => (compile_error!(stringify!(Error in newtype list < $d($error)* >)));
// 
//             // branch when something was found
//             (@yes) => ($($($yes)*)?);
//         }
//         contains_intern!(@rec $(($($list)*))*);
//     );
// 
//     // error handling, be nice and give a hint
//     (dollar_sign ($d:tt) if () $($_:tt)*) => (compile_error!(stringify!(Syntax error: if needs a non empty list)));
// 
//     // entry point
//     (if $($rest:tt)*) => (
//         // need to pass $ because of https://github.com/rust-lang/rust/issues/35853
//         $crate::contains!(dollar_sign ($) if $($rest)*);
//     );
// 
//     // error handling
//     (&($error:tt)*) => (compile_error!(stringify!(Syntax error in contains! macro < $d($error)* >)));
// }
// 
// #[test]
// fn contains_macro() {
//     contains!(if ((foo bar)) in () then () else ());
//     contains!(if ((foo)) in ((foo)(bar)) then (println!("yes")) else (println!("no")));
//     contains!(if ((foo)) in ((foo)(bar)(baz)) then (println!("yes")) else (println!("no")));
//     contains!(if ((foo)) in ((foo)(bar)(baz)) then (println!("yes")) else (println!("no")));
//     contains!(if ((bar)) in ((foo)(bar)(baz)) then (println!("yes")) else (println!("no")));
//     contains!(if ((baz)) in ((foo)(bar)(baz)) then (println!("yes")) else (println!("no")));
//     contains!(if ((barf)) in ((foo)(bar)(baz)) then (println!("yes")) else (println!("no")));
//     contains!(if ((barf)(bar)) in ((foo)(bar)(baz)) then (println!("yes")) else (println!("no")));
//     contains!(if ((foo)(bar)) in ((foo)(bar)(baz)) then (println!("yes")) else (println!("no")));
// 
//     contains!(if ((foo)) in ((foo)(bar)));
//     contains!(if ((foo)) in ((foo)(bar)) then (println!("yes")));
//     contains!(if ((barf)) in ((foo)(bar)(baz)) else (println!("no")));
// }
// 
// /// Extracts 'fn name', 'trait name' and the negations, return them as list
// /// usable with the `contains!()` macro.
// #[doc(hidden)]
// #[macro_export]
// macro_rules! defs_to_list {
//     // matching all function defs
//     (!fn $name:ident;$($tail:tt)*) => (
//         //show!(!fn $name);
//         (!fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($pub:vis fn $name:ident$(<$($generics:tt),*>)?$(($($params:tt)*) $(-> $ret:ty)?)?; $($tail:tt)*) => (
//         //show!(fn $name);
//         (fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($pub:vis fn $name:ident$(<$($generics:tt),*>)?$(($($params:tt)*) $(-> $ret:ty)?)?$block:block $($tail:tt)*) => (
//         //show!(fn $name);
//         (fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($pub:vis const fn $name:ident$(<$($generics:tt),*>)?$(($($params:tt)*) $(-> $ret:ty)?)?; $($tail:tt)*) => (
//         //show!(fn $name);
//         (fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($pub:vis const fn $name:ident$(<$($generics:tt),*>)?$(($($params:tt)*) $(-> $ret:ty)?)?$block:block $($tail:tt)*) => (
//         //show!(fn $name);
//         (fn $name)
//         $crate::defs_to_list!($($tail)*)
//     );
// 
//     // matching all trait defs
//     (!trait $name:ident;$($tail:tt)*) => (
//         //show!(!trait $name);
//         (!trait $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     (trait$(<$($generics1:tt),*>)? $name:ident$(<$($generics2:tt),*>)?;$($tail:tt)*) => (
//         //show!(trait $name);
//         (trait $name)
//         $crate::defs_to_list!($($tail)*)
//     );
//     (trait$(<$($generics1:tt),*>)? $name:ident$(<$($generics2:tt),*>)?$block:block $($tail:tt)*) => (
//         //show!(trait $name);
//         (trait $name)
//         $crate::defs_to_list!($($tail)*)
//     );
// 
//     // end of recursion, excess semicolon or error
//     () => ();
//     (; $($tail:tt)*) => (
//         $crate::defs_to_list!($($tail)*)
//     );
//     ($($error:tt)*) => (compile_error!(stringify!(Error in newtype "defs" block at < $($error)* > )));
// }
// 
// // This works only when the '//show!(..' lines are uncommented and the real output is commented
// // #[test]
// // fn defs_to_list_test() {
// //     defs_to_list! {
// //         !fn baz;
// //         fn foo;
// //         fn bar {}
// //         pub const fn pfn { 42 }
// //         fn more();
// //         fn more1(&self);
// //         fn more2() -> i32;
// //         fn more3() -> i32 {42}
// //         fn generic<'a,T>(&self, t:T) -> U;
// //         !trait Bar;
// //         trait Foo;
// //         trait Baz { fn baz(){} }
// //         trait<'a, T> Baz<'a,T>;
// //         trait<'a, T> Baz<'a,T> { fn baz(){} };;
// //        };
// // }