Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 /*
2 Copyright ⓒ 2016 macro-attr contributors.
3
4 Licensed under the MIT license (see LICENSE or <http://opensource.org
5 /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
6 <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
7 files in the project carrying such notice may not be copied, modified,
8 or distributed except according to those terms.
9 */
10 /*!
11 This crate provides the `macro_attr!` macro that enables the use of custom, macro-based attributes and derivations. Supercedes the `custom_derive` crate.
12
13 <style type="text/css">
14 .link-block { font-family: "Fira Sans"; }
15 .link-block > p { display: inline-block; }
16 .link-block > p > strong { font-weight: 500; margin-right: 1em; }
17 .link-block > ul { display: inline-block; padding: 0; list-style: none; }
18 .link-block > ul > li {
19 font-size: 0.8em;
20 background-color: #eee;
21 border: 1px solid #ccc;
22 padding: 0.3em;
23 display: inline-block;
24 }
25 </style>
26 <span></span><div class="link-block">
27
28 **Links**
29
30 * [Latest Release](https://crates.io/crates/macro-attr/)
31 * [Latest Docs](https://docs.rs/crate/macro-attr/)
32 * [Repository](https://github.com/DanielKeep/rust-custom-derive)
33
34 <span></span></div>
35
36 ## Compatibility
37
38 `macro-attr` is compatible with Rust 1.2 and higher.
39
40 ## Quick Example
41
42 To use it, make sure you link to the crate like so:
43
44 ```rust
45 #[macro_use] extern crate macro_attr;
46 # macro_rules! Dummy { (() struct $name:ident;) => {}; }
47 # macro_attr! { #[derive(Clone, Dummy!)] struct Foo; }
48 # fn main() { let _ = Foo; }
49 ```
50
51 The `macro_attr!` macro should be used to wrap an entire *single* item (`enum`, `struct`, *etc.*) declaration, including its attributes (both `derive` and others). All attributes and derivations which whose names end with `!` will be assumed to be implemented by macros, and treated accordingly.
52
53 For example:
54
55 ```rust
56 #[macro_use] extern crate macro_attr;
57
58 // Define some traits to be derived.
59
60 trait TypeName {
61 fn type_name() -> &'static str;
62 }
63
64 trait ReprType {
65 type Repr;
66 }
67
68 // Define macros which derive implementations of these macros.
69
70 macro_rules! TypeName {
71 // We can support any kind of item we want.
72 (() $(pub)* enum $name:ident $($tail:tt)*) => { TypeName! { @impl $name } };
73 (() $(pub)* struct $name:ident $($tail:tt)*) => { TypeName! { @impl $name } };
74
75 // Inner rule to cut down on repetition.
76 (@impl $name:ident) => {
77 impl TypeName for $name {
78 fn type_name() -> &'static str { stringify!($name) }
79 }
80 };
81 }
82
83 macro_rules! ReprType {
84 // Note that we use a "derivation argument" here for the `$repr` type.
85 (($repr:ty) $(pub)* enum $name:ident $($tail:tt)*) => {
86 impl ReprType for $name {
87 type Repr = $repr;
88 }
89 };
90 }
91
92 // Here is a macro that *modifies* the item.
93
94 macro_rules! rename_to {
95 (
96 ($new_name:ident),
97 then $cb:tt,
98 $(#[$($attrs:tt)*])*
99 enum $_old_name:ident $($tail:tt)*
100 ) => {
101 macro_attr_callback! {
102 $cb,
103 $(#[$($attrs)*])*
104 enum $new_name $($tail)*
105 }
106 };
107 }
108
109 macro_attr! {
110 #[allow(dead_code)]
111 #[derive(Clone, Copy, Debug, ReprType!(u8), TypeName!)]
112 #[rename_to!(Bar)]
113 #[repr(u8)]
114 enum Foo { A, B }
115 }
116
117 fn main() {
118 let bar = Bar::B;
119 let v = bar as <Bar as ReprType>::Repr;
120 let msg = format!("{}: {:?} ({:?})", Bar::type_name(), bar, v);
121 assert_eq!(msg, "Bar: B (1)");
122 }
123 ```
124 */
125 #![cfg_attr(not(feature = "std"), no_std)]
126
127 /**
128 When given an item definition, including its attributes, this macro parses said attributes and dispatches any attributes or derivations suffixed with `!` to user-defined macros. This allows multiple macros to process the same item.
129
130 This is similar to, but distinct from, the function of "procedural" macros and compiler plugins.
131
132 # Supported Forms
133
134 In particular, this macro looks for two kinds of syntax:
135
136 - Derivations such as the `Name` in `#[derive(Name!)]` or `#[derive(Name!(...))]`.
137 - Top-level attributes written as `#[name!]` or `#![name!(...)]`.
138
139 Unlike "real" attributes, optional parenthesised arguments after the `!` are allowed to be entirely arbitrary token trees, meaning they can effectively contain any token sequence. These are supported to allow custom attribute macros to easily take arguments.
140
141 Derivations parse the item and emit whatever additional definitions needed. They *cannot* change the item itself, and do not receive any other attributes attached to the item.
142
143 Attributes receive *everything* lexically *after* themselves, and must re-emit the item. This allows attributes to make changes to the item, drop or alter other attributes, *etc.*. This power makes writing attribute macros more difficult, however.
144
145 # Macro Derivations
146
147 Given the following input:
148
149 ```ignore
150 #[derive(Copy, Name!(args...), Clone, Another!, Debug)]
151 struct Foo;
152 ```
153
154 `macro_attr!` will expand to the equivalent of:
155
156 ```ignore
157 #[derive(Copy, Clone, Debug)]
158 struct Foo;
159
160 Name!((args...) struct Foo;);
161 Another!(() struct Foo;);
162 ```
163
164 Note that macro derives may be mixed with regular derives, or put in their own `#[derive(...)]` attribute. Also note that macro derive invocations are *not* passed the other attributes on the item; input will consist of the arguments provided to the derivation (*i.e.* `(args...)` in this example), the item's visibility (if any), and the item definition itself.
165
166 A macro derivation invoked *without* arguments will be treated as though it was invoked with empty parentheses. *i.e.* `#[derive(Name!)]` is equivalent to `#[derive(Name!())]`.
167
168 A derivation macro may expand to any number of new items derived from the provided input. There is no way for a derivation macro to alter the item itself (for that, use a macro attribute).
169
170 # Macro Attributes
171
172 When `macro_attr!` encounters an attribute suffixed with a `!` (*e.g.* `#[name!(args...)]`), it invokes the macro `name!` with everything lexically *after* that attribute. A macro attribute is free to add to, remove from, or alter the provided input as it sees fit, before instructing `macro_attr!` to resume parsing.
173
174 For example, given the following input:
175
176 ```ignore
177 #[make_unitary!]
178 #[repr(C)]
179 #[rename_to!(Quux)]
180 #[doc="Test."]
181 struct Bar { field: i32 }
182 ```
183
184 `macro_attr!` will expand to:
185
186 ```ignore
187 make_unitary! {
188 (), then $resume,
189 #[repr(C)]
190 #[rename_to!(Quux)]
191 #[doc="Test."]
192 struct Bar { field: i32 };
193 }
194 ```
195
196 Note that `$resume` is **not** literal. When implementing an attribute macro, you should accept this part as `$resume:tt`, and not attempt to inspect or deconstruct the contents.
197
198 Assuming `make_unitary!` removes the body of the `struct` it is attached to, `macro_attr!` *requires* that it expand to:
199
200 ```ignore
201 macro_attr_callback! {
202 $resume,
203 #[repr(C)]
204 #[rename_to!(Quxx)]
205 #[doc="Test."]
206 struct Bar;
207 }
208 ```
209
210 `macro_attr!` will then resume parsing, and expand to:
211
212 ```ignore
213 rename_to! {
214 (Quxx), then $resume,
215 #[doc="Test."]
216 struct Bar;
217 }
218 ```
219
220 Assuming `rename_to!` does the obvious thing and changes the name of the item it is attached to, it should expand to:
221
222 ```ignore
223 macro_attr_callback! {
224 $resume,
225 #[doc="Test."]
226 struct Quxx;
227 }
228 ```
229
230 Once more, `macro_attr!` will resume, and produce the final expansion of:
231
232 ```ignore
233 #[repr(C)]
234 #[doc="Test."]
235 struct Quxx;
236 ```
237
238 Note that normal attributes are automatically carried through and re-attached to the item.
239
240 Macro attributes should be used as sparingly as possible: due to the way Rust macros work, they must expand recursively in sequence, which can quickly consume the available macro recursion limit. This limit can be raised, but it makes for a less-than-ideal user experience if you are authoring macros to be used by others.
241 */
242 #[macro_export]
243 macro_rules! macro_attr {
244 ($($item:tt)*) => {
245 macro_attr_impl! { $($item)* }
246 };
247 }
248
249 /**
250 This macro exists as an implementation detail. This is because if it *wasn't*, then the public-facing `macro_attr!` macro's documentation would be hideously unwieldy.
251 */
252 #[doc(hidden)]
253 #[macro_export]
254 macro_rules! macro_attr_impl {
255 /*
256
257 > **Convention**: a capture named `$fixed` is used for any part of a recursive rule that is needed in the terminal case, but is not actually being *used* for the recursive part. This avoids having to constantly repeat the full capture pattern (and makes changing it easier).
258
259 # Primary Invocation Forms
260
261 These need to catch any valid form of item.
262
263 */
264 (
265 $(#[$($attrs:tt)*])*
266 const $($it:tt)*
267 ) => {
268 macro_attr_impl! {
269 @split_attrs
270 ($(#[$($attrs)*],)*), (), (),
271 (const $($it)*)
272 }
273 };
274
275 (
276 $(#[$($attrs:tt)*])*
277 enum $($it:tt)*
278 ) => {
279 macro_attr_impl! {
280 @split_attrs
281 ($(#[$($attrs)*],)*), (), (),
282 (enum $($it)*)
283 }
284 };
285
286 (
287 $(#[$($attrs:tt)*])*
288 extern $($it:tt)*
289 ) => {
290 macro_attr_impl! {
291 @split_attrs
292 ($(#[$($attrs)*],)*), (), (),
293 (extern $($it)*)
294 }
295 };
296
297 (
298 $(#[$($attrs:tt)*])*
299 fn $($it:tt)*
300 ) => {
301 macro_attr_impl! {
302 @split_attrs
303 ($(#[$($attrs)*],)*), (), (),
304 (fn $($it)*)
305 }
306 };
307
308 (
309 $(#[$($attrs:tt)*])*
310 impl $($it:tt)*
311 ) => {
312 macro_attr_impl! {
313 @split_attrs
314 ($(#[$($attrs)*],)*), (), (),
315 (impl $($it)*)
316 }
317 };
318
319 (
320 $(#[$($attrs:tt)*])*
321 mod $($it:tt)*
322 ) => {
323 macro_attr_impl! {
324 @split_attrs
325 ($(#[$($attrs)*],)*), (), (),
326 (mod $($it)*)
327 }
328 };
329
330 (
331 $(#[$($attrs:tt)*])*
332 pub $($it:tt)*
333 ) => {
334 macro_attr_impl! {
335 @split_attrs
336 ($(#[$($attrs)*],)*), (), (),
337 (pub $($it)*)
338 }
339 };
340
341 (
342 $(#[$($attrs:tt)*])*
343 static $($it:tt)*
344 ) => {
345 macro_attr_impl! {
346 @split_attrs
347 ($(#[$($attrs)*],)*), (), (),
348 (static $($it)*)
349 }
350 };
351
352 (
353 $(#[$($attrs:tt)*])*
354 struct $($it:tt)*
355 ) => {
356 macro_attr_impl! {
357 @split_attrs
358 ($(#[$($attrs)*],)*), (), (),
359 (struct $($it)*)
360 }
361 };
362
363 (
364 $(#[$($attrs:tt)*])*
365 trait $($it:tt)*
366 ) => {
367 macro_attr_impl! {
368 @split_attrs
369 ($(#[$($attrs)*],)*), (), (),
370 (trait $($it)*)
371 }
372 };
373
374 (
375 $(#[$($attrs:tt)*])*
376 type $($it:tt)*
377 ) => {
378 macro_attr_impl! {
379 @split_attrs
380 ($(#[$($attrs)*],)*), (), (),
381 (type $($it)*)
382 }
383 };
384
385 (
386 $(#[$($attrs:tt)*])*
387 use $($it:tt)*
388 ) => {
389 macro_attr_impl! {
390 @split_attrs
391 ($(#[$($attrs)*],)*), (), (),
392 (use $($it)*)
393 }
394 };
395
396 /*
397
398 # `@split_attrs`
399
400 This is responsible for dividing all attributes on an item into two groups:
401
402 - `#[derive(...)]`
403 - Everything else.
404
405 As part of this, it also explodes `#[derive(A, B(..), C, ...)]` into `A, B(..), C, ...`. This is to simplify the next stage.
406
407 */
408 (
409 @split_attrs
410 (),
411 $non_derives:tt,
412 $derives:tt,
413 $it:tt
414 ) => {
415 macro_attr_impl! {
416 @split_derive_attrs
417 { $non_derives, $it },
418 $derives,
419 (),
420 ()
421 }
422 };
423
424 (
425 @split_attrs
426 (#[derive($($new_drvs:tt)*)], $(#[$($attrs:tt)*],)*),
427 $non_derives:tt,
428 ($($derives:tt)*),
429 $it:tt
430 ) => {
431 macro_attr_impl! {
432 @split_attrs
433 ($(#[$($attrs)*],)*),
434 $non_derives,
435 ($($derives)* $($new_drvs)*,),
436 $it
437 }
438 };
439
440 (
441 @split_attrs
442 (#[$mac_attr:ident!], $(#[$($attrs:tt)*],)*),
443 $non_derives:tt,
444 $derives:tt,
445 ($($it:tt)*)
446 ) => {
447 $mac_attr! {
448 (),
449 then (macro_attr_impl! {
450 @split_attrs_resume
451 $non_derives,
452 $derives,
453 }),
454 $(#[$($attrs)*])*
455 $($it)*
456 }
457 };
458
459
460 (
461 @split_attrs
462 (#[$mac_attr:ident!($($attr_args:tt)*)], $(#[$($attrs:tt)*],)*),
463 $non_derives:tt,
464 $derives:tt,
465 ($($it:tt)*)
466 ) => {
467 $mac_attr! {
468 ($($attr_args)*),
469 then (macro_attr_impl! {
470 @split_attrs_resume
471 $non_derives,
472 $derives,
473 }),
474 $(#[$($attrs)*])*
475 $($it)*
476 }
477 };
478
479 (
480 @split_attrs
481 (#[$mac_attr:ident~!], $(#[$($attrs:tt)*],)*),
482 ($($non_derives:tt)*),
483 $derives:tt,
484 ($($it:tt)*)
485 ) => {
486 macro_attr_if_proc_macros! {
487 proc_macros: {
488 macro_attr_impl! {
489 @split_attrs
490 ($(#[$($attrs)*],)*),
491 ($($non_derives)* #[$mac_attr],),
492 $derives,
493 $($it)*
494 }
495 }
496 fallback: {
497 $mac_attr! {
498 (),
499 then (macro_attr_impl! {
500 @split_attrs_resume
501 ($($non_derives)*),
502 $derives,
503 }),
504 $(#[$($attrs)*])*
505 $($it)*
506 }
507 }
508 }
509 };
510
511 (
512 @split_attrs
513 (#[$mac_attr:ident~!($($attr_args:tt)*)], $(#[$($attrs:tt)*],)*),
514 ($($non_derives:tt)*),
515 $derives:tt,
516 ($($it:tt)*)
517 ) => {
518 macro_attr_if_proc_macros! {
519 proc_macros: {
520 macro_attr_impl! {
521 @split_attrs
522 ($(#[$($attrs)*],)*),
523 ($($non_derives)* #[$mac_attr($($attr_args)*)],),
524 $derives,
525 $($it)*
526 }
527 }
528 fallback: {
529 $mac_attr! {
530 ($($attr_args)*),
531 then (macro_attr_impl! {
532 @split_attrs_resume
533 ($($non_derives)*),
534 $derives,
535 }),
536 $(#[$($attrs)*])*
537 $($it)*
538 }
539 }
540 }
541 };
542
543 (
544 @split_attrs
545 (#[$new_attr:meta], $(#[$($attrs:tt)*],)*),
546 ($($non_derives:tt)*),
547 $derives:tt,
548 $it:tt
549 ) => {
550 macro_attr_impl! {
551 @split_attrs
552 ($(#[$($attrs)*],)*),
553 ($($non_derives)* #[$new_attr],),
554 $derives,
555 $it
556 }
557 };
558
559
560 /*
561
562 # `@split_attrs_resume`
563
564 Callback used to re-enter this macro after running a macro attribute.
565
566 */
567
568 (
569 @split_attrs_resume
570 $non_derives:tt,
571 $derives:tt,
572 $(#[$($attrs:tt)*])*
573 const $($it:tt)*
574 ) => {
575 macro_attr_impl! {
576 @split_attrs
577 ($(#[$($attrs)*],)*),
578 $non_derives,
579 $derives,
580 (const $($it)*)
581 }
582 };
583
584 (
585 @split_attrs_resume
586 $non_derives:tt,
587 $derives:tt,
588 $(#[$($attrs:tt)*])*
589 enum $($it:tt)*
590 ) => {
591 macro_attr_impl! {
592 @split_attrs
593 ($(#[$($attrs)*],)*),
594 $non_derives,
595 $derives,
596 (enum $($it)*)
597 }
598 };
599
600 (
601 @split_attrs_resume
602 $non_derives:tt,
603 $derives:tt,
604 $(#[$($attrs:tt)*])*
605 extern $($it:tt)*
606 ) => {
607 macro_attr_impl! {
608 @split_attrs
609 ($(#[$($attrs)*],)*),
610 $non_derives,
611 $derives,
612 (extern $($it)*)
613 }
614 };
615
616 (
617 @split_attrs_resume
618 $non_derives:tt,
619 $derives:tt,
620 $(#[$($attrs:tt)*])*
621 fn $($it:tt)*
622 ) => {
623 macro_attr_impl! {
624 @split_attrs
625 ($(#[$($attrs)*],)*),
626 $non_derives,
627 $derives,
628 (fn $($it)*)
629 }
630 };
631
632 (
633 @split_attrs_resume
634 $non_derives:tt,
635 $derives:tt,
636 $(#[$($attrs:tt)*])*
637 impl $($it:tt)*
638 ) => {
639 macro_attr_impl! {
640 @split_attrs
641 ($(#[$($attrs)*],)*),
642 $non_derives,
643 $derives,
644 (impl $($it)*)
645 }
646 };
647
648 (
649 @split_attrs_resume
650 $non_derives:tt,
651 $derives:tt,
652 $(#[$($attrs:tt)*])*
653 mod $($it:tt)*
654 ) => {
655 macro_attr_impl! {
656 @split_attrs
657 ($(#[$($attrs)*],)*),
658 $non_derives,
659 $derives,
660 (mod $($it)*)
661 }
662 };
663
664 (
665 @split_attrs_resume
666 $non_derives:tt,
667 $derives:tt,
668 $(#[$($attrs:tt)*])*
669 pub $($it:tt)*
670 ) => {
671 macro_attr_impl! {
672 @split_attrs
673 ($(#[$($attrs)*],)*),
674 $non_derives,
675 $derives,
676 (pub $($it)*)
677 }
678 };
679
680 (
681 @split_attrs_resume
682 $non_derives:tt,
683 $derives:tt,
684 $(#[$($attrs:tt)*])*
685 static $($it:tt)*
686 ) => {
687 macro_attr_impl! {
688 @split_attrs
689 ($(#[$($attrs)*],)*),
690 $non_derives,
691 $derives,
692 (static $($it)*)
693 }
694 };
695
696 (
697 @split_attrs_resume
698 $non_derives:tt,
699 $derives:tt,
700 $(#[$($attrs:tt)*])*
701 struct $($it:tt)*
702 ) => {
703 macro_attr_impl! {
704 @split_attrs
705 ($(#[$($attrs)*],)*),
706 $non_derives,
707 $derives,
708 (struct $($it)*)
709 }
710 };
711
712 (
713 @split_attrs_resume
714 $non_derives:tt,
715 $derives:tt,
716 $(#[$($attrs:tt)*])*
717 trait $($it:tt)*
718 ) => {
719 macro_attr_impl! {
720 @split_attrs
721 ($(#[$($attrs)*],)*),
722 $non_derives,
723 $derives,
724 (trait $($it)*)
725 }
726 };
727
728 (
729 @split_attrs_resume
730 $non_derives:tt,
731 $derives:tt,
732 $(#[$($attrs:tt)*])*
733 type $($it:tt)*
734 ) => {
735 macro_attr_impl! {
736 @split_attrs
737 ($(#[$($attrs)*],)*),
738 $non_derives,
739 $derives,
740 (type $($it)*)
741 }
742 };
743
744 (
745 @split_attrs_resume
746 $non_derives:tt,
747 $derives:tt,
748 $(#[$($attrs:tt)*])*
749 use $($it:tt)*
750 ) => {
751 macro_attr_impl! {
752 @split_attrs
753 ($(#[$($attrs)*],)*),
754 $non_derives,
755 $derives,
756 (use $($it)*)
757 }
758 };
759
760
761 /*
762
763 # `@split_derive_attrs`
764
765 This is responsible for taking the list of derivation attributes and splitting them into "built-in" and "custom" groups.
766
767 A custom attribute is any which has a `!` after the name, like a macro.
768 */
769
770 (@split_derive_attrs
771 { ($(#[$($non_derives:tt)*],)*), ($($it:tt)*) },
772 ($(,)*), (), ($($user_drvs:tt)*)
773 ) => {
774 macro_attr_impl! {
775 @as_item
776 $(#[$($non_derives)*])*
777 $($it)*
778 }
779
780 macro_attr_impl! {
781 @expand_user_drvs
782 ($($user_drvs)*), ($($it)*)
783 }
784 };
785
786 (@split_derive_attrs
787 { ($(#[$($non_derives:tt)*],)*), ($($it:tt)*) },
788 ($(,)*), ($($bi_drvs:ident,)+), ($($user_drvs:tt)*)
789 ) => {
790 macro_attr_impl! {
791 @as_item
792 #[derive($($bi_drvs,)+)]
793 $(#[$($non_derives)*])*
794 $($it)*
795 }
796
797 macro_attr_impl! {
798 @expand_user_drvs
799 ($($user_drvs)*), ($($it)*)
800 }
801 };
802
803 (@split_derive_attrs
804 $fixed:tt,
805 (,, $($tail:tt)*), $bi_drvs:tt, $user_drvs:tt
806 ) => {
807 macro_attr_impl! {
808 @split_derive_attrs
809 $fixed, ($($tail)*), $bi_drvs, $user_drvs
810 }
811 };
812
813 (@split_derive_attrs
814 $fixed:tt,
815 (, $($tail:tt)*), $bi_drvs:tt, $user_drvs:tt
816 ) => {
817 macro_attr_impl! {
818 @split_derive_attrs
819 $fixed, ($($tail)*), $bi_drvs, $user_drvs
820 }
821 };
822
823 /*
824
825 ## Custom Derivations
826
827 Now we can handle the custom derivations. There are two forms we care about: those *with* an argument, and those *without*.
828
829 The *reason* we care is that, in order to simplify the derivation macros, we want to detect the argument-less case and generate an empty pair of parens.
830
831 */
832 (@split_derive_attrs
833 $fixed:tt,
834 ($new_user:ident ! ($($new_user_args:tt)*), $($tail:tt)*), $bi_drvs:tt, ($($user_drvs:tt)*)
835 ) => {
836 macro_attr_impl! {
837 @split_derive_attrs
838 $fixed, ($($tail)*), $bi_drvs, ($($user_drvs)* $new_user($($new_user_args)*),)
839 }
840 };
841
842 (@split_derive_attrs
843 $fixed:tt,
844 ($new_user:ident !, $($tail:tt)*), $bi_drvs:tt, ($($user_drvs:tt)*)
845 ) => {
846 macro_attr_impl! {
847 @split_derive_attrs
848 $fixed, ($($tail)*), $bi_drvs, ($($user_drvs)* $new_user(),)
849 }
850 };
851
852 /*
853
854 ## Hybrid Derivations
855
856 These are derivations that use regular macros *or* procedural macros, depending on the version of Rust in use.
857
858 */
859 (@split_derive_attrs
860 $fixed:tt,
861 ($new_drv:ident ~!, $($tail:tt)*), ($($bi_drvs:ident,)*), ($($user_drvs:tt)*)
862 ) => {
863 macro_attr_if_proc_macros! {
864 proc_macros: {
865 macro_attr_impl! {
866 @split_derive_attrs
867 $fixed,
868 ($($tail)*),
869 ($($bi_drvs,)* $new_drv,),
870 ($($user_drvs)*)
871 }
872 }
873 fallback: {
874 macro_attr_impl! {
875 @split_derive_attrs
876 $fixed,
877 ($($tail)*),
878 ($($bi_drvs,)*),
879 ($($user_drvs)* $new_drv(),)
880 }
881 }
882 }
883 };
884
885 /*
886
887 ## Non-Macro Derivations
888
889 All the rest.
890
891 */
892 (@split_derive_attrs
893 $fixed:tt,
894 ($drv:ident, $($tail:tt)*), ($($bi_drvs:ident,)*), $user_drvs:tt
895 ) => {
896 macro_attr_impl! {
897 @split_derive_attrs
898 $fixed,
899 ($($tail)*), ($($bi_drvs,)* $drv,), $user_drvs
900 }
901 };
902
903 /*
904
905 # `@expand_user_drvs`
906
907 Finally, we have a recursive rule for expanding user derivations. This is basically just using the derivation name as a macro identifier.
908
909 This *has* to be recursive because we need to expand two independent repetition sequences simultaneously, and this causes `macro_rules!` to throw a wobbly. Don't want that. So, recursive it is.
910
911 */
912 (@expand_user_drvs
913 (), ($($it:tt)*)
914 ) => {};
915
916 (@expand_user_drvs
917 ($user_drv:ident $arg:tt, $($tail:tt)*), ($($it:tt)*)
918 ) => {
919 $user_drv! { $arg $($it)* }
920 macro_attr_impl! {
921 @expand_user_drvs
922 ($($tail)*), ($($it)*)
923 }
924 };
925
926 /*
927
928 # Miscellaneous Rules
929
930 */
931 (@as_item $($i:item)*) => {$($i)*};
932 }
933
934 /**
935 This macro invokes a "callback" macro, merging arguments together.
936
937 It takes an arbitrary macro call `(name!(args...))`, plus some sequence of `new_args...`, and expands `name!(args... new_args...)`.
938
939 Importantly, it works irrespective of the kind of grouping syntax used for the macro arguments, simplifying macros which need to *capture* callbacks.
940 */
941 #[macro_export]
942 macro_rules! macro_attr_callback {
943 (
944 ($cb:ident ! { $($cb_fixed:tt)* }),
945 $($args:tt)*
946 ) => {
947 $cb! { $($cb_fixed)* $($args)* }
948 };
949
950 (
951 ($cb:ident ! [ $($cb_fixed:tt)* ]),
952 $($args:tt)*
953 ) => {
954 $cb! [ $($cb_fixed)* $($args)* ]
955 };
956
957 (
958 ($cb:ident ! ( $($cb_fixed:tt)* )),
959 $($args:tt)*
960 ) => {
961 $cb! ( $($cb_fixed)* $($args)* )
962 };
963 }
964
965 /**
966 This macro provides a simple way to select between two branches of code, depending on whether or not support for procedural macros is enabled or not.
967 */
968 #[doc(hidden)]
969 #[macro_export]
970 #[cfg(feature="unstable-macros-1-1")]
971 macro_rules! macro_attr_if_proc_macros {
972 (
973 proc_macros: { $($items:item)* }
974 fallback: $_ignore:tt
975 ) => {
976 $($items)*
977 };
978 }
979
980 /**
981 This macro provides a simple way to select between two branches of code, depending on whether or not support for procedural macros is enabled or not.
982 */
983 #[doc(hidden)]
984 #[macro_export]
985 #[cfg(not(feature="unstable-macros-1-1"))]
986 macro_rules! macro_attr_if_proc_macros {
987 (
988 proc_macros: $_ignore:tt
989 fallback: { $($items:item)* }
990 ) => {
991 $($items)*
992 };
993 }