gen_ops 0.4.0

Macros for operator overloading for generic types
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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
#![no_std]
//! Macros for operator overloading of generic types. 
//! 
//! # Usage
//! 
//! The macros need four statements 
//! 
//! 1. (Optional) Generic parameter names ([See](crate#1-generic-parameter-names))
//! 2. Type signature or extended type signature([See](crate#2-type-signature-or-extended-type-signature))
//! 3. Callable expressions for each operator, and optionally, a where clause to be applied for the impl([See](crate#3-callable-expressions-for-each-operator))
//! 4. (Optional) Where clause to be applied for all impls([See](crate#4-where-clause-for-generic-parameters))
//! 
//! > **Note:** All statements end with a semicolon except the where clause.
//! 
//! 
//! ## Example
//! 
//! ```rust
//! # #[macro_use] extern crate gen_ops;
//! # use std::ops::{Add, Sub};
//! 
//! #[derive(Debug, Copy, Clone, PartialEq)]
//! struct Pair<T>(pub T, pub T);
//! 
//! #[inline]
//! fn sub_pair<T>(a: &Pair<T>, b: &Pair<T>) -> Pair<T>
//! where T: Sub<Output=T> + Copy {
//!     Pair(a.0 - b.0, a.1 - b.1)
//! }
//! 
//! gen_ops!(
//!     <T>;                               // Generic parameter names
//!     types Pair<T>, Pair<T> => Pair<T>; // Type signature
//!     for + call |a: &Pair<T>, b: &Pair<T>| {
//!         Pair(a.0 + b.0, a.1 + b.1)
//!     };
//!     for - call sub_pair;               // Callable expressions for operators
//!     where T: Add<Output=T> + Sub<Output=T> + Copy
//! );
//! 
//! # fn main() {
//! let a = Pair(2, 3);
//! let b = Pair(1, 8);
//! 
//! println!("a + b = {:?}", a + b); //a + b = Pair(3, 11)
//! # assert_eq!(a + b, Pair(3, 11));
//! println!("a - b = {:?}", a - b); //a - b = Pair(1, -5)
//! # assert_eq!(a-b, Pair(1, -5));
//! # }
//! ```
//! 
//! 
//! ## 1. Generic parameter names
//! 
//! First statement is Genetic parameters for `impl`. 
//! 
//! ```rust, ignore
//! gen_ops!(
//!     <T, U, V>;
//!     .....
//! );
//! 
//! // results in
//! 
//! impl<T, U, V> .... {
//!     ....
//! }
//! ```
//! 
//! ### Lifetime params
//! 
//! Lifetime params must be added at the beginning.
//! 
//! ```rust, ignore
//! gen_ops!(
//!     <'a, 'b, T, U, V>;
//!     .....
//! );
//! 
//! // results in
//! 
//! impl<'a, 'b, T, U, V> .... {
//!     ....
//! }
//! ```
//! 
//! ### Const params
//! 
//! To add const params add a `|` followed by the const parameters. The const params must be at the end 
//! and `|` is mandatory even if the const params are the only parameters. 
//! 
//! ```rust, ignore
//! gen_ops!(
//!     <T, U, V | const SZ:usize, const DEF: i32>;
//!     .....
//! );
//! 
//! // results in
//! 
//! impl<T, U, V, const SZ: usize, const DEF: i32> .... {
//!     ....
//! }
//! 
//! //and 
//! gen_ops!(
//!     <| const SZ:usize, const DEF: i32>; // `|` is mandatory
//!     .....
//! );
//! 
//! // results in
//! 
//! impl<const SZ: usize, const DEF: i32> .... {
//!     ....
//! }
//! 
//! ```
//! 
//! ### Constraints
//! 
//! Type constraints are not supported. Use [where clause][wherecls] instead.
//! 
//! 
//! ## 2. Type signature or extended type signature 
//! 
//! Type signatures are used by [`gen_ops!`] and [`gen_ops_comm!`]. 
//! And extended type signatures are used by [`gen_ops_ex!`] 
//! and [`gen_ops_comm_ex!`]. 
//! 
//! ### Syntax
//! - For binary operators the signature is written as `types Lhs, Rhs => Out;`. 
//! - For assignment operators the signature is written as `types Lhs, Rhs;`. 
//! - For unary operators the signature is written as `types Lhs => Out;`. 
//! 
//! Type signatures can have borrowed types for `Lhs` and `Rhs`, e.g, `types &Lhs, &mut Rhs => Out;`. 
//! 
//! ### What is extended type signature?
//! 
//! Extended type signatures, however, cannot use borrowed types. 
//! The extended types signatures can have `ref` or `mut` modifiers for `Lhs` and `Rhs`. 
//! 
//! `ref Lhs` implies that the trait is implemented for both `& Lhs` and `Lhs`. 
//! `mut Lhs` implies that the trait is implemented for `&mut Lhs`, `&Lhs` and `Lhs`. 
//! 
//! For example, `types ref T, ref T;` will implement `types &T, &T;`, `types &T, T;`, 
//! `types T, &T;` and `types T, T`. 
//! 
//! 
//! ## 3. Callable expressions for each operator
//! 
//! Callable expression statements are written as `for <operator> call <expr>;`
//! 
//! The expression can be a closure or a function. 
//! 
//! The closure or function must take immutable borrowed parameters except for 
//! assignment operators where the first parameter must be mutable. 
//! 
//! ### Example
//! ```
//! # #[macro_use] extern crate gen_ops;
//! struct A(i32);
//! struct B(f32);
//! struct C(f64);
//! 
//! fn sub_abc(a:&A, b:&B) -> C {
//!     C(b.0 as f64 - a.0 as f64)
//! }
//! 
//! gen_ops!(
//!     types A, B => C;
//!     for + call |a: &A, b: &B| C(b.0 as f64 + a.0 as f64);
//!     for - call sub_abc;
//! );
//! # fn main() {
//! # let a = A(5);
//! # let b = B(4.);
//! # assert_eq!((a + b).0, 9.);
//! # }
//! ```
//! ### Doc Strings
//! 
//! You can add doc string above each `for` statements. 
//! 
//! ```rust, ignore
//! gen_ops!(
//!     ...
//!     /// Docs here
//!     /// Docs here
//!     for + call ....
//! 
//!     /// Docs here
//!     for - call ...
//! )
//! ```
//! 
//! ### Where clause for each operator
//! 
//! Optionally you can add where clause to be used only with one operator. 
//! Add this statement after the semicolon and enclosed in parentheses. 
//! ### Example
//! ```
//! # #[macro_use] extern crate gen_ops;
//! # use std::ops::*;
//! struct Complex<T>(T, T);
//! 
//! gen_ops_ex!(
//!     <T>;
//!     types ref Complex<T>, ref Complex<T> => Complex<T>;
//!     for + call |a: &Complex<T>, b: &Complex<T>| Complex(a.0+b.0, a.1+b.1);
//!     (where T: Add<T, Output=T>)               //applies to + impls only
//!     for - call |a: &Complex<T>, b: &Complex<T>| Complex(a.0-b.0, a.1-b.1);
//!     (where T: Sub<T, Output=T>)               //applies to - impls only
//! 
//!     where T: Copy //applied to all impls
//! );
//! ```
//! 
//! Also note that there is no need for semicolon after the individual where clauses.
//!
//! ## 4. Where clause for all impls
//! 
//! Optionally you can add a where clause as the last statement. 
//! This will be applied to all generated impls. 
//! Note that the where clause does not end with a semicolon. 
//! 
//! ```rust, ignore
//! gen_ops!(
//!     <T>;
//!     ...
//!     where T: Copy
//! );
//! 
//! // results in
//! 
//! impl<T> ... where T: Copy {
//!     ...
//! }
//! ```
//! 
//! # Why use/not use gen_ops crate?
//! 
//! ## Features
//! - Multiple operators can be implemented with one macro call. 
//! - Use closure or function for the implementation
//! - Supports generics
//! 
//! ## Limitations
//! - These macros use [TT munching](https://veykril.github.io/tlborm/decl-macros/patterns/tt-muncher.html)
//! which slows down compilation.
//! 
//! # Quick Reference Tables
//! 
//! ## Binary 
//! 
//! | Macro                | Type sign. | Lhs type sign.        | Rhs type sign.        | `Lhs` = `Rhs` |
//! |:---------------------|:----------:|:---------------------:|:---------------------:|:-------------:|
//! | [`gen_ops!`]         | borrowed   | `L`, `&L`, `&mut L`   | `R`, `&R`, `&mut R`   | allowed       |
//! | [`gen_ops_ex!`]      | [extended][exts]   | `L`, `ref L`, `mut L` | `R`, `ref R`, `mut R` | allowed       |
//! | [`gen_ops_comm!`]    | borrowed   | `L`, `&L`, `&mut L`   | `R`, `&R`, `&mut R`   | not allowed   |
//! | [`gen_ops_comm_ex!`] | [extended][exts]   | `L`, `ref L`, `mut L` | `R`, `ref R`, `mut R` | not allowed   |
//! 
//! ## Assignment
//! 
//! | Macro                | Type sign. | Lhs type sign. | Rhs type sign. |
//! |:---------------------|:----------:|:--------------:|:--------------:|
//! | [`gen_ops!`]         | borrowed   | `L`, `&mut L`  | `R`, `&mut R`  |
//! | [`gen_ops_ex!`]      | [extended][exts]   | `L`, `mut L`   | `R`, `mut R`   |
//! 
//! ## Unary
//! 
//! | Macro                | Type sign. | Lhs type sign.        |
//! |:---------------------|:----------:|:---------------------:|
//! | [`gen_ops!`]         | borrowed   | `L`, `&L`, `&mut L`   |
//! | [`gen_ops_ex!`]      | [extended][exts]   | `L`, `ref L`, `mut L` |
//! 
//! ## Closure/function
//! 
//! | Operator | Lhs        | Rhs    | Return |
//! |:--------:|:----------:|:------:|:------:|
//! | binary   | `&Lhs`     | `&Rhs` | `Out`  |
//! | assign   | `&mut Lhs` | `&Rhs` | `()`   |
//! | unary    | `&Lhs`     | -      | `Out`  |
//! 
//! 
//! [exts]: crate#what-is-extended-type-signature
//! [wherecls]: crate#4-where-clause-for-all-impls

#[macro_use]
mod util;

#[macro_use]
mod core_impl;

#[macro_use]
mod gen_ops_bin;

#[macro_use]
mod gen_ops_un;

#[macro_use]
mod gen_ops_asgn;

/// The primary macro for all operators. 
/// 
/// # Example
///
/// ```rust
/// # #[macro_use] extern crate gen_ops;
/// # use std::ops::{Add, Sub};
/// #[derive(Debug, Copy, Clone, PartialEq)]
/// struct Pair<T>(pub T, pub T);
/// 
/// gen_ops!(
///     <T>;
///     types Pair<T>, Pair<T> => Pair<T>;
///     for + call |a: &Pair<T>, b: &Pair<T>| Pair(a.0 + b.0, a.1 + b.1);
///     (where T: Add<Output=T>)
///
///     for - call |a: &Pair<T>, b: &Pair<T>| Pair(a.0 - b.0, a.1 - b.1);
///     (where T: Sub<Output=T>)
/// 
///     where T: Copy
/// );
/// 
/// # fn main() {
/// let a = Pair(10, 5);
/// let b = Pair(8, 9);
/// 
/// println!("a + b = {:?}", a + b); // a + b = Pair(18, 14)
/// # assert_eq!(a+b, Pair(18, 14));
/// println!("a - b = {:?}", a - b); // a - b = Pair(2, -4)
/// # assert_eq!(a-b, Pair(2, -4));
/// # }
/// ```
/// 
/// # Note
/// 
/// Type signatures supported for [`gen_ops!`] are 
/// - `types Lhs, Rhs => Out;` for binary
/// - `types Lhs, Rhs;` for assignment
/// - `types Lhs => Out;` for unary
/// 
/// `Lhs` and `Rhs` can be borrowed types except for assignment operators where `Lhs` cannot be 
/// immutably borrowed. 
/// 
/// For unary and binary operators, the callable expressions must take immutable borrowed types as argument(s) 
/// and return the result of the `Out` type. 
/// And for assignment operators the callable expressions must take mutable borrowed type as first 
/// argument, take immutable borrowed type as second argument and return nothing;
/// 
#[macro_export]
macro_rules! gen_ops {
    ($(<$($($lt:lifetime),+)? $(,)? $($($gen:ident),+)? $(| $(const $C:ident : $Ct:ty),+)?>;)? types $($rest:tt)+) => {
        gen_ops!(@step1 ($(<$($($lt),+,)? $($($gen),+ ,)? $($(const $C : $Ct),+)?>)?); types $($rest)+);
    };

    //binary
    (@step1 ($($gen:tt)*); types $lhs:ty, $rhs:ty => $out:ty;$($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs own own;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types &$lhs:ty, $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs ref_ own;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types $lhs:ty, &$rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs own ref_;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*);types &$lhs:ty, &$rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs ref_ ref_;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types &mut $lhs:ty, $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs mut_ own;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types $lhs:ty, &mut $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs own mut_;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types &mut $lhs:ty, &mut $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs mut_ mut_;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types &mut $lhs:ty, &$rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs mut_ ref_;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types &$lhs:ty, &mut $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs ref_ mut_;
            $($rest)+
        );
    };

    //assign
    (@step1 ($($gen:tt)*); types $lhs:ty, $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs own own;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types $lhs:ty, &$rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs own ref_;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types $lhs:ty, &mut $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs own mut_;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types mut $lhs:ty, $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs mut_ own;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types mut $lhs:ty, &$rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs mut_ ref_;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types mut $lhs:ty, &mut $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs mut_ mut_;
            $($rest)+
        );
    };

    //unary
    (@step1 ($($gen:tt)*); types $lhs:ty => $output:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_un!(
            ($($gen)*);
            types $lhs => $output;
            refs own;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types &$lhs:ty => $output:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_un!(
            ($($gen)*);
            types $lhs => $output;
            refs ref_;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types &mut$lhs:ty => $output:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_un!(
            ($($gen)*);
            types $lhs => $output;
            refs mut_;
            $($rest)+
        );
    };
}


/// Implements commutative operations. 
/// 
/// # Example
///
/// ```rust
/// # #[macro_use] extern crate gen_ops;
/// # use std::ops::{Mul, BitAnd};
/// #[derive(Debug, Copy, Clone, PartialEq)]
/// struct Pair<T>(pub T, pub T);
/// 
/// gen_ops_comm!(
///     <T>;
///     types Pair<T>, i32 => Pair<T>;
///     for * call |a: &Pair<T>, b:&i32| Pair(a.0 * *b, a.1 * *b);
///     (where T: Mul<i32, Output=T>)
/// 
///     for & call |a: &Pair<T>, b:&i32| Pair(a.0 & *b, a.1 & *b);
///     (where T: BitAnd<i32, Output=T>)
/// 
///     where T: Copy
/// );
/// 
/// # fn main() {
/// let a = Pair(12, 3);
/// 
/// println!("a * 5 = {:?}", a * 5); //a * 5 = Pair(60, 15)
/// # assert_eq!(a*5, Pair(60, 15));
/// println!("5 * a = {:?}", 5 * a); //5 * a = Pair(60, 15)
/// # assert_eq!(5* a, Pair(60, 15));
/// println!("a & 2 = {:?}", a & 2); //a & 2 = Pair(0, 2)
/// # assert_eq!(a & 2, Pair(0, 2));
/// println!("2 & a = {:?}", 2 & a); //2 & a = Pair(0, 2)
/// # assert_eq!(2 & a, Pair(0, 2));
/// # }
/// ```
/// 
/// # Note
/// 
/// The only type signature supported for [`gen_ops_comm!`] is `types Lhs, Rhs => Out;`  
/// It implements both `types Lhs, Rhs => Out;` and `types Rhs, Lhs => Out;`. 
/// 
/// `Lhs` and `Rhs` can be borrowed types except for assignment operators where `Lhs` cannot be 
/// immutably borrowed. 
/// Also make sure that `Lhs` and `Rhs` are of different types. 
/// 
/// Callable expressions must take immutable borrowed types as arguments 
/// and return the result of the `Out` type
/// 
#[macro_export]
macro_rules! gen_ops_comm {
    ($(<$($($lt:lifetime),+)? $(,)? $($($gen:ident),+)? $(| $(const $C:ident : $Ct:ty),+)?>;)? types $($rest:tt)+) => {
        gen_ops_comm!(@step1 ($(<$($($lt),+,)? $($($gen),+ ,)? $($(const $C : $Ct),+)?>)?); types $($rest)+);
    };
    (@step1 ($($gen:tt)*); types $lhs:ty, $($rest:tt)+) => {
        $crate::gen_ops_comm!(@step2 ($($gen)*); types own: $lhs, $($rest)+);
    };
    (@step1 ($($gen:tt)*); types &$lhs:ty, $($rest:tt)+) => {
        $crate::gen_ops_comm!(@step2 ($($gen)*); types ref_: $lhs, $($rest)+);
    };
    (@step1 ($($gen:tt)*); types &mut $lhs:ty, $($rest:tt)+) => {
        $crate::gen_ops_comm!(@step2 ($($gen)*); types mut_: $lhs, $($rest)+);
    };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); 
            types $lhs, $rhs => $out; 
            refs $lref own;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); 
            types $rhs, $lhs => $out; 
            refs own $lref rev;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, &$rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); 
            types $lhs, $rhs => $out; 
            refs $lref ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); 
            types $rhs, $lhs => $out; 
            refs ref_ $lref rev;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, &mut $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); 
            types $lhs, $rhs => $out; 
            refs $lref mut_;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); 
            types $rhs, $lhs => $out; 
            refs mut_ $lref rev;
            $($rest)+
        );
    };
}


/// Implements trait for borrowed types. 
/// 
/// # Example
///
/// ```rust
/// # #[macro_use] extern crate gen_ops;
/// # use std::ops::Mul;
/// #[derive(Debug, Copy, Clone, PartialEq)]
/// struct Pair<T>(pub T, pub T);
/// 
/// gen_ops_ex!(
///     <T>;
///     types mut Pair<T>, T => Pair<T>;
///     for * call |a: &Pair<T>, b:&T| Pair(a.0 * *b, a.1 * *b);
///     where T: Mul<Output=T> + Copy
/// );
/// 
/// # fn main() {
/// let mut a = Pair(12, 3);
/// {
///     let mut b = &mut a;
///     println!("&mut a * 2 = {:?}", b * 2);// &mut a * 2 = Pair(24, 6)
/// }
/// println!("&a * 2 = {:?}", &a * 2);// &a * 2 = Pair(24, 6)
/// # assert_eq!(&a * 2, Pair(24, 6));
/// println!("a * 2 = {:?}", a * 2);// a * 2 = Pair(24, 6)
/// # assert_eq!(a * 2, Pair(24, 6));
/// # }
/// ```
/// 
/// # Note
/// 
/// [`gen_ops_ex!`] uses [extended] type signature. 
/// 
/// Type signatures supported for [`gen_ops_ex!`] are 
/// - `types Lhs, Rhs => Out;` for binary 
/// - `types Lhs, Rhs;` for assignment 
/// - `types Lhs => Out` for unary 
/// 
/// `Lhs` and `Rhs` must be owned types. 
/// 
/// For unary and binary operators, the callable expressions must take immutable borrowed types 
/// as argument(s) and return the result of the `Out` type. 
/// And for assignment operators the callable expressions must take mutable borrowed type as first 
/// argument, take immutable borrowed type as second argument and return nothing;
/// 
/// [extended]: crate#2-type-signature-or-extended-type-signature
#[macro_export]
macro_rules! gen_ops_ex {
    ($(<$($($lt:lifetime),+)? $(,)? $($($gen:ident),+)? $(| $(const $C:ident : $Ct:ty),+)?>;)? types $($rest:tt)+) => {
        gen_ops_ex!(@step1 ($(<$($($lt),+,)? $($($gen),+ ,)? $($(const $C : $Ct),+)?>)?); types $($rest)+);
    };
    (@step1 ($($gen:tt)*); types $lhs:ty, $($rest:tt)+) => {
        $crate::gen_ops_ex!(@step2 ($($gen)*); types own: $lhs, $($rest)+);
    };
    (@step1 ($($gen:tt)*); types ref $lhs:ty, $($rest:tt)+) => {
        $crate::gen_ops_ex!(@step2 ($($gen)*); types ref_: $lhs, $($rest)+);
        $crate::gen_ops_ex!(@step2 ($($gen)*); types own: $lhs, $($rest)+);
    };
    (@step1 ($($gen:tt)*); types mut $lhs:ty, $($rest:tt)+) => {
        $crate::gen_ops_ex!(@step2 ($($gen)*); types mut_: $lhs, $($rest)+);
        $crate::gen_ops_ex!(@step2 ($($gen)*); types ref_: $lhs, $($rest)+);
        $crate::gen_ops_ex!(@step2 ($($gen)*); types own: $lhs, $($rest)+);
    };
    (@step1 ($($gen:tt)*); types mut $lhs:ty, $($rest:tt)+) => {
        $crate::gen_ops_ex!(@step2 ($($gen)*); types mut_: $lhs, $($rest)+);
        $crate::gen_ops_ex!(@step2 ($($gen)*); types ref_: $lhs, $($rest)+);
        $crate::gen_ops_ex!(@step2 ($($gen)*); types own: $lhs, $($rest)+);
    };

    //binary
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs $lref own;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, ref $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs $lref ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs $lref own;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, mut $rhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs $lref mut_;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs $lref ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*);
            types $lhs, $rhs => $out;
            refs $lref own;
            $($rest)+
        );
    };

    //assign
    (@step2 ($($gen:tt)*); types own: $lhs:ty, $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs own own;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types own: $lhs:ty, ref $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs own ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs own own;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types own: $lhs:ty, mut $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs own mut_;
            $($rest)+
        );
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs own ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs own own;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types mut_: $lhs:ty, $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs mut_ own;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types mut_: $lhs:ty, ref $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs mut_ ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs mut_ own;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types mut_: $lhs:ty, mut $rhs:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs mut_ mut_;
            $($rest)+
        );
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs mut_ ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_asgn!(
            ($($gen)*);
            types $lhs, $rhs;
            refs mut_ own;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types ref_: $lhs:ty, mut $rhs:ty; $($rest:tt)+) => {};
    (@step2 ($($gen:tt)*); types ref_: $lhs:ty, ref $rhs:ty; $($rest:tt)+) => {};
    (@step2 ($($gen:tt)*); types ref_: $lhs:ty, $rhs:ty; $($rest:tt)+) => {};

    //unary
    (@step1 ($($gen:tt)*); types $lhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_un!(
            ($($gen)*);
            types $lhs => $out;
            refs own;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types ref $lhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_un!(
            ($($gen)*);
            types $lhs => $out;
            refs ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_un!(
            ($($gen)*);
            types $lhs => $out;
            refs own;
            $($rest)+
        );
    };
    (@step1 ($($gen:tt)*); types mut $lhs:ty => $out:ty; $($rest:tt)+) => {
        $crate::_gen_ops_internal_un!(
            ($($gen)*);
            types $lhs => $out;
            refs mut_;
            $($rest)+
        );
        $crate::_gen_ops_internal_un!(
            ($($gen)*);
            types $lhs => $out;
            refs ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_un!(
            ($($gen)*);
            types $lhs => $out;
            refs own;
            $($rest)+
        );
    };
}

/// Implements commutative operations for borrowed types. 
/// 
/// # Example
///
/// ```rust
/// # #[macro_use] extern crate gen_ops;
/// # use std::ops::{Mul, BitAnd};
/// #[derive(Debug, Copy, Clone, PartialEq)]
/// struct Pair<T>(pub T, pub T);
/// 
/// gen_ops_comm_ex!(
///     <T>;
///     types ref Pair<T>, i32 => Pair<T>;
///     for * call |a: &Pair<T>, b:&i32| Pair(a.0 * *b, a.1 * *b);
///     where T: Mul<i32, Output=T> + Copy
/// );
/// 
/// # fn main() {
/// let a = Pair(12, 3);
/// println!("a * 5 = {:?}", a * 5); //a * 5 = Pair(60, 15)
/// # assert_eq!(a * 5, Pair(60, 15));
/// println!("5 * a = {:?}", 5 * a); //5 * a = Pair(60, 15)
/// # assert_eq!(5 * a, Pair(60, 15));
/// println!("5 * &a = {:?}", 5 * &a); //5 * &a = Pair(60, 15)
/// # assert_eq!(5 * &a, Pair(60, 15));
/// println!("&a * 5 = {:?}", &a * 5); //&a * 5 = Pair(60, 15)
/// # assert_eq!(&a * 5, Pair(60, 15));
/// # }
/// ```
/// 
/// # Note
/// 
/// [`gen_ops_comm_ex!`] uses [extended] type signature. The only type signature 
/// supported for [`gen_ops_comm_ex!`] is `types Lhs, Rhs => Out;`. 
/// It implements both `types Lhs, Rhs => Out;` and `types Rhs, Lhs => Out;`. 
/// 
/// `Lhs` and `Rhs` must be owned types 
/// and must be of different types. 
/// 
/// Callable expressions must take immutable borrowed types as arguments 
/// and return the result of the `Out` type
/// 
/// [extended]: crate#2-type-signature-or-extended-type-signature
#[macro_export]
macro_rules! gen_ops_comm_ex {
    ($(<$($($lt:lifetime),+)? $(,)? $($($gen:ident),+)? $(| $(const $C:ident : $Ct:ty),+)?>;)? types $($rest:tt)+) => {
        gen_ops_comm_ex!(@step1 ($(<$($($lt),+,)? $($($gen),+ ,)? $($(const $C : $Ct),+)?>)?); types $($rest)+);
    };
    (@step1 ($($gen:tt)*); types $lhs:ty,  $($rest:tt)+) => {
        $crate::gen_ops_comm_ex!(@step2 ($($gen)*); types own: $lhs, $($rest)+);
    };
    (@step1 ($($gen:tt)*); types ref $lhs:ty,  $($rest:tt)+) => {
        $crate::gen_ops_comm_ex!(@step2 ($($gen)*); types ref_: $lhs, $($rest)+);
        $crate::gen_ops_comm_ex!(@step2 ($($gen)*); types own: $lhs, $($rest)+);
    };
    (@step1 ($($gen:tt)*); types mut $lhs:ty,  $($rest:tt)+) => {
        $crate::gen_ops_comm_ex!(@step2 ($($gen)*); types mut_: $lhs, $($rest)+);
        $crate::gen_ops_comm_ex!(@step2 ($($gen)*); types ref_: $lhs, $($rest)+);
        $crate::gen_ops_comm_ex!(@step2 ($($gen)*); types own: $lhs, $($rest)+);
    };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, $rhs:ty => $out:ty;  $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $lhs, $rhs => $out;
            refs $lref own;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $rhs, $lhs => $out;
            refs own $lref rev;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, ref $rhs:ty => $out:ty;  $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $lhs, $rhs => $out;
            refs $lref ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $rhs, $lhs => $out;
            refs ref_ $lref rev;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $lhs, $rhs => $out;
            refs $lref own;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $rhs, $lhs => $out;
            refs own $lref rev;
            $($rest)+
        );
    };
    (@step2 ($($gen:tt)*); types $lref:ident: $lhs:ty, mut $rhs:ty => $out:ty;  $($rest:tt)+) => {
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $lhs, $rhs => $out;
            refs $lref mut_;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $rhs, $lhs => $out;
            refs mut_ $lref rev;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $lhs, $rhs => $out;
            refs $lref ref_;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $rhs, $lhs => $out;
            refs ref_ $lref rev;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $lhs, $rhs => $out;
            refs $lref own;
            $($rest)+
        );
        $crate::_gen_ops_internal_bin!(
            ($($gen)*); types $rhs, $lhs => $out;
            refs own $lref rev;
            $($rest)+
        );
    };
}