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 990 991 992 993
/* Copyright ⓒ 2016 macro-attr contributors. Licensed under the MIT license (see LICENSE or <http://opensource.org /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All files in the project carrying such notice may not be copied, modified, or distributed except according to those terms. */ /*! This crate provides the `macro_attr!` macro that enables the use of custom, macro-based attributes and derivations. Supercedes the `custom_derive` crate. <style type="text/css"> .link-block { font-family: "Fira Sans"; } .link-block > p { display: inline-block; } .link-block > p > strong { font-weight: 500; margin-right: 1em; } .link-block > ul { display: inline-block; padding: 0; list-style: none; } .link-block > ul > li { font-size: 0.8em; background-color: #eee; border: 1px solid #ccc; padding: 0.3em; display: inline-block; } </style> <span></span><div class="link-block"> **Links** * [Latest Release](https://crates.io/crates/macro-attr/) * [Latest Docs](https://docs.rs/crate/macro-attr/) * [Repository](https://github.com/DanielKeep/rust-custom-derive) <span></span></div> ## Compatibility `macro-attr` is compatible with Rust 1.2 and higher. ## Quick Example To use it, make sure you link to the crate like so: ```rust #[macro_use] extern crate macro_attr; # macro_rules! Dummy { (() struct $name:ident;) => {}; } # macro_attr! { #[derive(Clone, Dummy!)] struct Foo; } # fn main() { let _ = Foo; } ``` 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. For example: ```rust #[macro_use] extern crate macro_attr; // Define some traits to be derived. trait TypeName { fn type_name() -> &'static str; } trait ReprType { type Repr; } // Define macros which derive implementations of these macros. macro_rules! TypeName { // We can support any kind of item we want. (() $(pub)* enum $name:ident $($tail:tt)*) => { TypeName! { @impl $name } }; (() $(pub)* struct $name:ident $($tail:tt)*) => { TypeName! { @impl $name } }; // Inner rule to cut down on repetition. (@impl $name:ident) => { impl TypeName for $name { fn type_name() -> &'static str { stringify!($name) } } }; } macro_rules! ReprType { // Note that we use a "derivation argument" here for the `$repr` type. (($repr:ty) $(pub)* enum $name:ident $($tail:tt)*) => { impl ReprType for $name { type Repr = $repr; } }; } // Here is a macro that *modifies* the item. macro_rules! rename_to { ( ($new_name:ident), then $cb:tt, $(#[$($attrs:tt)*])* enum $_old_name:ident $($tail:tt)* ) => { macro_attr_callback! { $cb, $(#[$($attrs)*])* enum $new_name $($tail)* } }; } macro_attr! { #[allow(dead_code)] #[derive(Clone, Copy, Debug, ReprType!(u8), TypeName!)] #[rename_to!(Bar)] #[repr(u8)] enum Foo { A, B } } fn main() { let bar = Bar::B; let v = bar as <Bar as ReprType>::Repr; let msg = format!("{}: {:?} ({:?})", Bar::type_name(), bar, v); assert_eq!(msg, "Bar: B (1)"); } ``` */ #![cfg_attr(not(feature = "std"), no_std)] /** 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. This is similar to, but distinct from, the function of "procedural" macros and compiler plugins. # Supported Forms In particular, this macro looks for two kinds of syntax: - Derivations such as the `Name` in `#[derive(Name!)]` or `#[derive(Name!(...))]`. - Top-level attributes written as `#[name!]` or `#![name!(...)]`. 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. 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. 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. # Macro Derivations Given the following input: ```ignore #[derive(Copy, Name!(args...), Clone, Another!, Debug)] struct Foo; ``` `macro_attr!` will expand to the equivalent of: ```ignore #[derive(Copy, Clone, Debug)] struct Foo; Name!((args...) struct Foo;); Another!(() struct Foo;); ``` 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. 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!())]`. 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). # Macro Attributes 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. For example, given the following input: ```ignore #[make_unitary!] #[repr(C)] #[rename_to!(Quux)] #[doc="Test."] struct Bar { field: i32 } ``` `macro_attr!` will expand to: ```ignore make_unitary! { (), then $resume, #[repr(C)] #[rename_to!(Quux)] #[doc="Test."] struct Bar { field: i32 }; } ``` 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. Assuming `make_unitary!` removes the body of the `struct` it is attached to, `macro_attr!` *requires* that it expand to: ```ignore macro_attr_callback! { $resume, #[repr(C)] #[rename_to!(Quxx)] #[doc="Test."] struct Bar; } ``` `macro_attr!` will then resume parsing, and expand to: ```ignore rename_to! { (Quxx), then $resume, #[doc="Test."] struct Bar; } ``` Assuming `rename_to!` does the obvious thing and changes the name of the item it is attached to, it should expand to: ```ignore macro_attr_callback! { $resume, #[doc="Test."] struct Quxx; } ``` Once more, `macro_attr!` will resume, and produce the final expansion of: ```ignore #[repr(C)] #[doc="Test."] struct Quxx; ``` Note that normal attributes are automatically carried through and re-attached to the item. 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. */ #[macro_export] macro_rules! macro_attr { ($($item:tt)*) => { macro_attr_impl! { $($item)* } }; } /** 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. */ #[doc(hidden)] #[macro_export] macro_rules! macro_attr_impl { /* > **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). # Primary Invocation Forms These need to catch any valid form of item. */ ( $(#[$($attrs:tt)*])* const $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (const $($it)*) } }; ( $(#[$($attrs:tt)*])* enum $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (enum $($it)*) } }; ( $(#[$($attrs:tt)*])* extern $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (extern $($it)*) } }; ( $(#[$($attrs:tt)*])* fn $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (fn $($it)*) } }; ( $(#[$($attrs:tt)*])* impl $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (impl $($it)*) } }; ( $(#[$($attrs:tt)*])* mod $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (mod $($it)*) } }; ( $(#[$($attrs:tt)*])* pub $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (pub $($it)*) } }; ( $(#[$($attrs:tt)*])* static $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (static $($it)*) } }; ( $(#[$($attrs:tt)*])* struct $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (struct $($it)*) } }; ( $(#[$($attrs:tt)*])* trait $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (trait $($it)*) } }; ( $(#[$($attrs:tt)*])* type $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (type $($it)*) } }; ( $(#[$($attrs:tt)*])* use $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), (), (), (use $($it)*) } }; /* # `@split_attrs` This is responsible for dividing all attributes on an item into two groups: - `#[derive(...)]` - Everything else. As part of this, it also explodes `#[derive(A, B(..), C, ...)]` into `A, B(..), C, ...`. This is to simplify the next stage. */ ( @split_attrs (), $non_derives:tt, $derives:tt, $it:tt ) => { macro_attr_impl! { @split_derive_attrs { $non_derives, $it }, $derives, (), () } }; ( @split_attrs (#[derive($($new_drvs:tt)*)], $(#[$($attrs:tt)*],)*), $non_derives:tt, ($($derives:tt)*), $it:tt ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, ($($derives)* $($new_drvs)*,), $it } }; ( @split_attrs (#[$mac_attr:ident!], $(#[$($attrs:tt)*],)*), $non_derives:tt, $derives:tt, ($($it:tt)*) ) => { $mac_attr! { (), then (macro_attr_impl! { @split_attrs_resume $non_derives, $derives, }), $(#[$($attrs)*])* $($it)* } }; ( @split_attrs (#[$mac_attr:ident!($($attr_args:tt)*)], $(#[$($attrs:tt)*],)*), $non_derives:tt, $derives:tt, ($($it:tt)*) ) => { $mac_attr! { ($($attr_args)*), then (macro_attr_impl! { @split_attrs_resume $non_derives, $derives, }), $(#[$($attrs)*])* $($it)* } }; ( @split_attrs (#[$mac_attr:ident~!], $(#[$($attrs:tt)*],)*), ($($non_derives:tt)*), $derives:tt, ($($it:tt)*) ) => { macro_attr_if_proc_macros! { proc_macros: { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), ($($non_derives)* #[$mac_attr],), $derives, $($it)* } } fallback: { $mac_attr! { (), then (macro_attr_impl! { @split_attrs_resume ($($non_derives)*), $derives, }), $(#[$($attrs)*])* $($it)* } } } }; ( @split_attrs (#[$mac_attr:ident~!($($attr_args:tt)*)], $(#[$($attrs:tt)*],)*), ($($non_derives:tt)*), $derives:tt, ($($it:tt)*) ) => { macro_attr_if_proc_macros! { proc_macros: { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), ($($non_derives)* #[$mac_attr($($attr_args)*)],), $derives, $($it)* } } fallback: { $mac_attr! { ($($attr_args)*), then (macro_attr_impl! { @split_attrs_resume ($($non_derives)*), $derives, }), $(#[$($attrs)*])* $($it)* } } } }; ( @split_attrs (#[$new_attr:meta], $(#[$($attrs:tt)*],)*), ($($non_derives:tt)*), $derives:tt, $it:tt ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), ($($non_derives)* #[$new_attr],), $derives, $it } }; /* # `@split_attrs_resume` Callback used to re-enter this macro after running a macro attribute. */ ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* const $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (const $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* enum $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (enum $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* extern $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (extern $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* fn $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (fn $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* impl $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (impl $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* mod $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (mod $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* pub $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (pub $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* static $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (static $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* struct $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (struct $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* trait $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (trait $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* type $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (type $($it)*) } }; ( @split_attrs_resume $non_derives:tt, $derives:tt, $(#[$($attrs:tt)*])* use $($it:tt)* ) => { macro_attr_impl! { @split_attrs ($(#[$($attrs)*],)*), $non_derives, $derives, (use $($it)*) } }; /* # `@split_derive_attrs` This is responsible for taking the list of derivation attributes and splitting them into "built-in" and "custom" groups. A custom attribute is any which has a `!` after the name, like a macro. */ (@split_derive_attrs { ($(#[$($non_derives:tt)*],)*), ($($it:tt)*) }, ($(,)*), (), ($($user_drvs:tt)*) ) => { macro_attr_impl! { @as_item $(#[$($non_derives)*])* $($it)* } macro_attr_impl! { @expand_user_drvs ($($user_drvs)*), ($($it)*) } }; (@split_derive_attrs { ($(#[$($non_derives:tt)*],)*), ($($it:tt)*) }, ($(,)*), ($($bi_drvs:ident,)+), ($($user_drvs:tt)*) ) => { macro_attr_impl! { @as_item #[derive($($bi_drvs,)+)] $(#[$($non_derives)*])* $($it)* } macro_attr_impl! { @expand_user_drvs ($($user_drvs)*), ($($it)*) } }; (@split_derive_attrs $fixed:tt, (,, $($tail:tt)*), $bi_drvs:tt, $user_drvs:tt ) => { macro_attr_impl! { @split_derive_attrs $fixed, ($($tail)*), $bi_drvs, $user_drvs } }; (@split_derive_attrs $fixed:tt, (, $($tail:tt)*), $bi_drvs:tt, $user_drvs:tt ) => { macro_attr_impl! { @split_derive_attrs $fixed, ($($tail)*), $bi_drvs, $user_drvs } }; /* ## Custom Derivations Now we can handle the custom derivations. There are two forms we care about: those *with* an argument, and those *without*. 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. */ (@split_derive_attrs $fixed:tt, ($new_user:ident ! ($($new_user_args:tt)*), $($tail:tt)*), $bi_drvs:tt, ($($user_drvs:tt)*) ) => { macro_attr_impl! { @split_derive_attrs $fixed, ($($tail)*), $bi_drvs, ($($user_drvs)* $new_user($($new_user_args)*),) } }; (@split_derive_attrs $fixed:tt, ($new_user:ident !, $($tail:tt)*), $bi_drvs:tt, ($($user_drvs:tt)*) ) => { macro_attr_impl! { @split_derive_attrs $fixed, ($($tail)*), $bi_drvs, ($($user_drvs)* $new_user(),) } }; /* ## Hybrid Derivations These are derivations that use regular macros *or* procedural macros, depending on the version of Rust in use. */ (@split_derive_attrs $fixed:tt, ($new_drv:ident ~!, $($tail:tt)*), ($($bi_drvs:ident,)*), ($($user_drvs:tt)*) ) => { macro_attr_if_proc_macros! { proc_macros: { macro_attr_impl! { @split_derive_attrs $fixed, ($($tail)*), ($($bi_drvs,)* $new_drv,), ($($user_drvs)*) } } fallback: { macro_attr_impl! { @split_derive_attrs $fixed, ($($tail)*), ($($bi_drvs,)*), ($($user_drvs)* $new_drv(),) } } } }; /* ## Non-Macro Derivations All the rest. */ (@split_derive_attrs $fixed:tt, ($drv:ident, $($tail:tt)*), ($($bi_drvs:ident,)*), $user_drvs:tt ) => { macro_attr_impl! { @split_derive_attrs $fixed, ($($tail)*), ($($bi_drvs,)* $drv,), $user_drvs } }; /* # `@expand_user_drvs` Finally, we have a recursive rule for expanding user derivations. This is basically just using the derivation name as a macro identifier. 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. */ (@expand_user_drvs (), ($($it:tt)*) ) => {}; (@expand_user_drvs ($user_drv:ident $arg:tt, $($tail:tt)*), ($($it:tt)*) ) => { $user_drv! { $arg $($it)* } macro_attr_impl! { @expand_user_drvs ($($tail)*), ($($it)*) } }; /* # Miscellaneous Rules */ (@as_item $($i:item)*) => {$($i)*}; } /** This macro invokes a "callback" macro, merging arguments together. It takes an arbitrary macro call `(name!(args...))`, plus some sequence of `new_args...`, and expands `name!(args... new_args...)`. Importantly, it works irrespective of the kind of grouping syntax used for the macro arguments, simplifying macros which need to *capture* callbacks. */ #[macro_export] macro_rules! macro_attr_callback { ( ($cb:ident ! { $($cb_fixed:tt)* }), $($args:tt)* ) => { $cb! { $($cb_fixed)* $($args)* } }; ( ($cb:ident ! [ $($cb_fixed:tt)* ]), $($args:tt)* ) => { $cb! [ $($cb_fixed)* $($args)* ] }; ( ($cb:ident ! ( $($cb_fixed:tt)* )), $($args:tt)* ) => { $cb! ( $($cb_fixed)* $($args)* ) }; } /** 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. */ #[doc(hidden)] #[macro_export] #[cfg(feature="unstable-macros-1-1")] macro_rules! macro_attr_if_proc_macros { ( proc_macros: { $($items:item)* } fallback: $_ignore:tt ) => { $($items)* }; } /** 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. */ #[doc(hidden)] #[macro_export] #[cfg(not(feature="unstable-macros-1-1"))] macro_rules! macro_attr_if_proc_macros { ( proc_macros: $_ignore:tt fallback: { $($items:item)* } ) => { $($items)* }; }