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
//! [](https://crates.io/crates/derive_aliases)
//! [](https://docs.rs/derive_aliases)
//! 
//! 
//! [](https://github.com/nik-rev/derive-aliases)
//!
//! This crate improves Rust's `derive` macro by supporting user-defined Derive aliases.
//!
//! ```toml
//! [dependencies]
//! derive_aliases = "0.4"
//! ```
//!
//! # Usage
//!
//! Define aliases using [`define!`](define), and use them with [`#[derive]`](derive):
//!
//! ```
//! mod derive_alias {
//! // Define the aliases
//! derive_aliases::define! {
//! Eq = ::core::cmp::PartialEq, ::core::cmp::Eq;
//! Ord = ..Eq, ::core::cmp::PartialOrd, ::core::cmp::Ord;
//! Copy = ::core::marker::Copy, ::core::clone::Clone;
//! }
//! }
//!
//! use derive_aliases::derive;
//!
//! // Use the aliases:
//! #[derive(Debug, ..Ord, ..Copy)]
//! struct User;
//! # fn main() {}
//! ```
//!
//! The above expands to this:
//!
//! ```
//! #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
//! struct User;
//! ```
//!
//! - `#[derive(..Eq)]`
//! - expands to `#[derive(::core::cmp::PartialEq, ::core::cmp::Eq)]`
//! - `#[derive(..Ord)]`
//! - expands to `#[derive(..Eq, ::core::cmp::PartialOrd, ::core::cmp::Ord)]`
//! - ...which expands to `#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::cmp::PartialOrd, ::core::cmp::Ord)]`
//!
//! **How it works:**
//!
//! - `derive_aliases::define!` expands to a bunch of `macro_rules!` items. Each macro item is the real alias
//! - `#[derive_aliases::derive]` expands to a bunch of calls to macros at `crate::derive_alias`
//!
//! # IDE Support
//!
//! Hovering over an alias `#[derive(..Alias)]` shows *exactly* what it expands into, and even Goto Definition directly brings you where the alias is defined.
//!
//! 
//!
//! # Tip
//!
//! To globally override `#[std::derive]` with [`#[derive_aliases::derive]`](derive), add the following:
//!
//! ```
//! #[macro_use]
//! extern crate derive_aliases;
//! ```
//!
//! The above lets you [`define!`](define) aliases and then use them anywhere in your crate!
//!
//! I have put a **ton** of effort into optimizing `derive_aliases` to be as zero-cost as possible in terms of compile-time over the standard library's `derive`,
//! so don't worry about any overhead of `#[derive_aliases::derive]` even when no aliases are used! `derive_aliases` has 0 dependencies (not even `quote` or `syn`!)
//!
//! # Derives are de-duplicated
//!
//! Each derive alias expands into a bunch of derives, then de-duplicated. If there are 2 or more of the same derive, only 1 is kept.
//! This is useful when there are some "pre-requisite" derives needed, but if they already exist then don't add them (instead of compile error'ing).
//!
//! ```rust
//! extern crate zerocopy;
//! # #[macro_use]
//! # extern crate derive_aliases;
//!
//! mod derive_alias {
//! derive_aliases::define! {
//! FastHash = ::zerocopy::ByteHash, ::zerocopy::Immutable, ::zerocopy::IntoBytes;
//! FastEq = ::zerocopy::ByteEq, ::zerocopy::Immutable, ::zerocopy::IntoBytes;
//! }
//! }
//!
//! # mod _1 {
//! #[derive(..FastHash)]
//! struct Example;
//! # }
//!
//! // expands to:
//! # mod _1a {
//! #[derive(::zerocopy::ByteHash, ::zerocopy::Immutable, ::zerocopy::IntoBytes)]
//! struct Example;
//! # }
//!
//!
//!
//! # mod _2 {
//! #[derive(..FastEq)]
//! struct Example;
//! # }
//!
//! // expands to:
//! # mod _2a {
//! #[derive(::zerocopy::ByteEq, ::zerocopy::Immutable, ::zerocopy::IntoBytes)]
//! struct Example;
//! # }
//!
//!
//!
//! # mod _3 {
//! #[derive(..FastEq, ..FastHash)]
//! struct Example;
//! # }
//!
//! // expands to:
//! # mod _3a {
//! #[derive(::zerocopy::ByteEq, ::zerocopy::ByteHash, ::zerocopy::Immutable, ::zerocopy::IntoBytes)]
//! struct Example;
//!
//! // note that the 2 `Immutable` and 2 `IntoBytes` derives were de-duplicated
//! # }
//! # fn main() {}
//! ```
//!
//! # Splitting up derive aliases
//!
//! All derive aliases must exist at your `crate::derive_alias`, so invoke the `derive_aliases::define!` macro there.
//!
//! You can break [`define!`](define) apart into multiple definitions:
//!
//! ```
//! # use derive_aliases::derive;
//! #
//! mod derive_alias {
//! mod foo {
//! derive_aliases::define! {
//! Eq = ::core::cmp::Eq, ::core::cmp::PartialEq;
//! Ord = ::core::cmp::PartialOrd, ::core::cmp::Ord, ..Eq;
//! }
//! }
//! mod bar {
//! derive_aliases::define! {
//! Copy = ::core::marker::Copy, ::core::clone::Clone;
//! StdTraits = ..Eq, ..Ord, ..Copy, ::core::fmt::Debug, ::core::hash::Hash;
//! }
//! }
//!
//! pub(crate) use foo::{Eq, Ord};
//! pub(crate) use bar::{Copy, StdTraits};
//! }
//!
//! #[derive(..StdTraits)]
//! struct User;
//! # fn main() {}
//! ```
//!
//! The above Just Works. Most importantly, derive aliases need to available at `crate::derive_alias`.
//!
//! # Sharing derives across multiple crates
//!
//! Use `#![export_derive_aliases]` inside of a call to [`derive_aliases::define!`](define) to allow aliases to be used in other crates:
//!
//! ```
//! pub mod derive_alias {
//! derive_aliases::define! {
//! #![export_derive_aliases]
//!
//! Eq = ::core::cmp::PartialEq, ::core::cmp::Eq;
//! Copy = ::core::marker::Copy, ::core::clone::Clone;
//! }
//! }
//! # fn main() {}
//! ```
//!
//! In another crate, import the aliases:
//!
//! ```
//! # macro_rules! x { () => {
//! // crate which contains `Eq` and `Ord` aliases
//! extern crate foo;
//!
//! pub mod derive_alias {
//! // import aliases from that crate
//! use foo::derive_alias::*;
//!
//! derive_aliases::define! {
//! Ord = ..Eq, ::core::cmp::PartialOrd, ::core::cmp::Ord;
//! }
//! }
//! # }; }
//! # fn main() {}
//! ```
//!
//! For details, hover over `#![export_derive_aliases]` in your editor
// NOTE on `#[cfg_attr(doc, doc = "...")]`:
//
// Documentation on an `#[doc(inline)] pub use item` concatenates with documentation of `item`
// in the generated HTML documentation. I want users to see documentation on hover and when they goto definition
// they see the actual documentation, but without having duplicate documentation in the HTML documentation
pub use define;
// NOTE on `#[cfg_attr(doc, doc = "...")]`:
//
// Documentation on an `#[doc(inline)] pub use item` concatenates with documentation of `item`
// in the generated HTML documentation. I want users to see documentation on hover and when they goto definition
// they see the actual documentation, but without having duplicate documentation in the HTML documentation
pub use derive;
/// This is the main macro that makes `derive_aliases` possible
///
/// At a high level:
///
/// - Invocation of `new_alias!` creates a new alias, which is a `macro_rules!`.
/// So an invocation of `derive_aliases::define!` expands to a bunch of invocations of `new_alias!` each of
/// which expand to `macro_rules!` alias
///
/// - The `#[derive_aliases::derive]` macro takes the item it is applied to `:item` and nests it inside of a
/// bunch of `macro_rules!` aliases located at `crate::derive_alias`
///
/// All of this must happen while keeping compile-times **extremely fast**.
///
/// # Example
///
/// Here's the simplest possible example. It defines an alias `..Eq` which expands to `Eq, PartialEq`. This macro will be invoked from `crate::derive_alias` module.
///
/// ```ignore
/// derive_aliases::define! {
/// Eq = ::core::cmp::Eq, ::core::cmp::PartialEq;
/// }
/// ```
///
/// The first expansion is this:
///
/// ```ignore
/// ::derive_aliases::__internal_derive_aliases_new_alias! {
/// "..." a $ Eq! [::core::cmp::PartialEq], [::core::cmp::Eq],
/// }
/// pub(crate) use Eq;
/// ```
///
/// - `"..."` contains the documentation, stuff passed to `#[doc = "..."]` so user gets a nice overview of what happens on hover,
/// but it is pretty long and so we omit it
///
/// - `a` is the "visibility mode" of the alias. `a` and `b` are the two possible modes. in `a` (the default) the macro is not
/// exported from the crate root, but in `b` it is. and this must be explicitly requested with the `#![export_derive_aliases]` attribute
///
/// In any case the derive `Alias` will be available at `crate::derive_alias::Alias`, but with `b` it is also available at the crate root (due to
/// a limitation of the declarative macros)
///
/// - Because `new_alias!` itself creates a `macro_rules!` we need the `$` token inside of the `macro_rules!` definition. But we can't use `$`
/// since that refers to the meta-variables captured by `new_alias!` and now the macro created *inside* `new_alias!`. So we do the 2nd
/// best thing and have `$_:tt` which is always `$`. This allows us to use `$_` instead of `$` inside of the macro definition
///
/// - Next is `Eq!`, which is the real name of the macro that we'll `pub use ... as Eq`. The `macro_rules!` alias defined by `new_alias!` will
/// refer to *itself* at exactly that position, as it will do some recursive TT munching by calling itself
///
/// - Next we have a bunch of paths inside of `[]`: `[$($path:tt)*]`. These paths are the *fully resolved* paths that the alias itself expands to.
/// They do *not* reference another alias. So given this:
///
/// ```ignore
/// derive_aliases::define! {
/// Eq = ::core::cmp::Eq, ::core::cmp::PartialEq;
/// Ord = ::core::cmp::Ord, ::core::cmp::PartialOrd, ..Eq;
/// }
/// ```
///
/// The `Ord` alias will expand to an invocation of `new_alias!` like this:
///
/// ```ignore
/// ::derive_aliases::__internal_derive_aliases_new_alias! {
/// "..." a $ Ord! [::core::cmp::PartialEq], [::core::cmp::Eq], [::core::cmp::Ord], [::core::cmp::PartialOrd],
/// }
/// pub(crate) use Ord;
/// ```
///
/// Because the `define!` macro itself referenes alias `..Eq` which it itself defines, we are able to "inline" all the aliases
/// to make it more performant. However, we also allow to refer to aliases that were **not** defined inside of the same `define!` call.
/// I'll explain how this is possible later.
///
/// **Back to `[$($path:tt)*]`**. We essentally use that instead of `$path:path` because you cannot compare 2 `:path` meta-variables. it's
/// as simple as that. We **must** compare them, but we can't. But we CAN compare 1 `[$($path:tt)*]` with another `[$($path:tt)*]`
///
/// **Why we need to compare**: Our aliases have the important property that they act as *sets*. 2 aliases that expand to a bunch of derives,
/// both expansions contain e.g. `::core::clone::Clone` derive. **But they can be used together**. They are merged like `HashSet`s.
///
/// **How the merger happens**: It's a recursion. Essentially, if our CURRENT alias adds `A` and `B` but we have `A, B, C, D`. We recurse to remove
/// `A` and `B` from the set to get `C, D`. Then we add `A, B` again to get `A, B, C, D`. If any of `A` or `B` already existed, they won't be added
/// again. If they didn't, they will be added now
///
/// Let's look at what `new_alias!` expands to now:
///
/// ```ignore
/// ::derive_aliases::__internal_derive_aliases_new_alias! {
/// "..." a $ Eq! [::core::cmp::PartialEq], [::core::cmp::Eq],
/// }
/// pub(crate) use Eq;
/// ```
///
/// The above expands into this:
///
/// ```ignore
/// macro_rules! Eq { /* ... */ }
/// pub(crate) use Eq;
/// ```
///
/// The insides of this `macro_rules!` alias are not shown, as it's pretty big. And it won't make much sense to try and
/// comprehend the generated `macro_rules!` alias. So instead let's see what an invocation of `#[derive]` expands to:
///
/// ```ignore
/// #[derive(Debug, ..Eq)]
/// struct Example;
/// ```
///
/// Expansion is this:
///
/// ```ignore
/// crate::derive_alias::Eq! { @[Debug,] [struct Example;] [] }
/// ```
///
/// 1. We collect all normal non-alias derives inside of `[...]`, so `Debug, Clone` will be collected as `Debug, Clone,`
/// 1. The actual `:item` that we apply this to is wrapped inside of `[]` entirely. This is incredibly efficient - we do **not**
/// parse the `:item` itself, so it is just `:tt`. We pass this `:tt` everywhere. It is a SINGLE `TokenTree`, so it is very, very
/// efficient to just pass it around. We only match on it as `[$($tt:tt)*]` to actually apply the alias at the END.
///
/// This is one of the KEY things that keeps compile-time for this macro extremely fast. We **do not** want to add any overhead **at all**.
/// 1. Finally we have the empty `[]`. this is the **accumulator**. As we resolve more and more derives, we'll add all of them to this `[]`
/// After we've resolved all the derives, we take all the things inside this `[]` and pass it to `#[std::deive]`.
/// 1. `crate::derive_alias::Eq!` is referencing the actual `macro_rules!` alias that we explained earlier. This macro knows that
/// it expands to derives `::core::cmp::PartialEq` and `::core::cmp::Eq` so it will add them to the `[]` accumulator mentioned previously
/// 1. The `@` is just a glyph to keep compile times very fast. The insides of `macro_rules!` alias `Eq` contain a lot of rules, but
/// since we start with `@` Rust's algorithm can discard all of the rules that do not match the first token, making compile-times very fast
///
/// Let's examine every stage of the expansion of the above:
///
/// ```ignore
/// crate::derive_alias::Eq! { @[Debug,] [struct Example;] [] }
///
/// crate::derive_alias::Eq! { ?[Debug,][struct Example;][][] }
///
/// #[::core::prelude::v1::derive(Debug, ::core::cmp::PartialEq, ::core::cmp::Eq)]
/// struct Example;
/// ```
///
/// # Example with derives that conflict
///
/// As you can see this expanion is **very** simple because it just directly expands to the `std::derive`.
/// However, it gets a lot more interesting when there are multiple aliases involved, and these aliases may contain
/// derives that are overlapping and should be merged.
///
/// Given these aliases:
///
/// ```ignore
/// derive_aliases::define! {
/// Eq = ::core::cmp::Eq, ::core::cmp::PartialEq;
/// Ord = ::core::cmp::Ord, ::core::cmp::PartialOrd, ..Eq;
/// }
/// ```
///
/// They'll expand to this:
///
/// ```ignore
/// ::derive_aliases::__internal_derive_aliases_new_alias! {
/// "..." a $ Ord! [::core::cmp::PartialEq], [::core::cmp::Eq], [::core::cmp::Ord], [::core::cmp::PartialOrd],
/// }
/// pub(crate) use Ord;
///
/// ::derive_aliases::__internal_derive_aliases_new_alias! {
/// "..." a $ Eq! [::core::cmp::PartialEq], [::core::cmp::Eq],
/// }
/// pub(crate) use Eq;
/// ```
///
/// Then expanding these aliases:
///
/// ```ignore
/// #[derive(Debug, ..Eq, ..Ord)]
/// struct Example;
/// ```
///
/// Will expand to this:
///
/// ```ignore
/// crate::derive_alias::Ord! { crate::derive_alias::Eq,(@[Debug,] [struct Example;]) [] }
/// ```
///
/// Now it's a little more interesting. We are nesting the aliases 1 inside the other. Let's examine
/// every state of their expansions (with `trace_macros!(true)`):
///
/// ```ignore
/// crate::derive_alias::Ord! { crate::derive_alias::Eq,(@[Debug,] [struct Example;]) [] }
/// crate::derive_alias::Ord! { #crate::derive_alias::Eq,(@[Debug,] [struct Example;]) [] [] }
///
/// // first, the Ord! simply inserts all of its expansions into our stack
/// crate::derive_alias::Eq! { @[Debug,] [struct Example;] [[::core::cmp::PartialEq],[::core::cmp::Eq],[::core::cmp::Ord],[::core::cmp::PartialOrd],] }
///
/// // alias `Eq` expands to `::core::cmp::Eq` and `::core::cmp::PartialEq`. Because these can't just be added (they conflict with each-other)
/// // we go through all existing derives in our stack and remove all of those that are exactly `::core::cmp::PartialEq` and `::core::cmp::Eq`
/// crate::derive_alias::Eq! { ?[Debug,] [struct Example;] [[::core::cmp::PartialEq],[::core::cmp::Eq],[::core::cmp::Ord],[::core::cmp::PartialOrd],] [] }
/// crate::derive_alias::Eq! { ?[Debug,] [struct Example;] [[::core::cmp::PartialEq],[::core::cmp::Eq],[::core::cmp::Ord],[::core::cmp::PartialOrd],] [] }
///
/// // Removed `PartialEq`
/// crate::derive_alias::Eq! { ?[Debug,] [struct Example;] [[::core::cmp::Eq],[::core::cmp::Ord],[::core::cmp::PartialOrd],] [] }
///
/// // Removed `Eq`
/// crate::derive_alias::Eq! { ?[Debug,] [struct Example;] [[::core::cmp::Ord],[::core::cmp::PartialOrd],] [] }
///
/// // Did NOT remove `Ord`, added it to the "good" pile (no conflicts)
/// crate::derive_alias::Eq! { ?[Debug,] [struct Example;] [[::core::cmp::PartialOrd],] [[::core::cmp::Ord],] }
///
/// // Did NOT remove `PartialOrd`, added it to the "good" pile (no conflicts)
/// crate::derive_alias::Eq! { ?[Debug,] [struct Example;] [] [[::core::cmp::Ord],[::core::cmp::PartialOrd],] }
///
/// // Now that our pile is totally empty, and we reached end of the expansion (signified by the `?` glyph)
/// // let's generate the real `#[std::derive]`
/// #[::core::prelude::v1::derive(Debug,::core::cmp::Ord,::core::cmp::PartialOrd,::core::cmp::PartialEq,::core::cmp::Eq,)] struct Example;
/// ```
///
/// What we learned:
///
/// 1. Each additional alias adds an extra level of nesting
/// 1. We resolve aliases 1-at-a-time. Each alias knows what derives it expands to.
/// 1. Each alias REMOVES all the same aliases that exist from the list of aliases to expand to.
/// 1. Once done, it adds it aliases to the end of the list
/// 1. The process is repeated for each alias
};
//
// Remove each derive from the set
$*
// Everything else is just added as-is
=> ;
///////////////////////////////////////////////////////////////
// DONE
///////////////////////////////////////////////////////////////
// Now that we've removed the traits we want to add, Add them.
// This guarantees there is NO duplicate of them here
//
// Copy! { ? [Debug,][struct Foo;] [] [[Ord], [PartialOrd], [PartialEq],] }
=> ;
// Remove each derive from the set
$*
// Everything else is just added as-is
=> ;
// Reached the base case. No more nested aliases
=> ;
///////////////////////////////////////////////////////////////
=> ;
///////////////////////////////////////////////////////////////
=> ;
// BASE CASE: Reached end of the alias accumulation, create
=> ;
}
}
}
}
pub use __internal_derive_aliases_new_alias_with_externs;