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
/// Declares a [type witness](crate#what-are-type-witnesses) enum.
///
#[doc = explain_type_witness!()]
///
/// [**examples below**](#examples)
///
/// # Generated items
///
/// This macro always generates:
///
/// - An enum with tuple variants, each of which has a single [`TypeEq`] field.
///
/// - Impls of [`Copy`] and [`Clone`] for the enum.
///
/// - An impl of [`TypeWitnessTypeArg`] for the enum.
///
/// - An impl of [`MakeTypeWitness`] for each variant of the enum.
///
/// Additional trait impls are generated when the [`derive(...)`](#derive) syntax is used.
///
/// ### Derivation
///
/// These impls are generated if you opt into them with the [`derive(...)`](#derive) syntax:
///
/// - `Debug`
/// - `PartialEq`
/// - `Eq`
/// - `PartialOrd`
/// - `Ord`
/// - `Hash`
///
/// As opposed to `#[derive(...))]`-generated implementations,
/// these impls don't require type parameters to implement the derived trait.
///
/// This macro always implements `Copy` and `Clone` for the declared type witness,
/// `derive(Copy, Clone)` does nothing.
///
/// # Syntax
///
/// This macro takes an enum-like syntax:
/// ```text
/// $(#[$enum_meta:meta])*
/// $(derive($($derive:ident),* $(,)?)))?
/// $vis:vis enum $enum:ident $(<$($generics:generics_param),* $(,)?>)?
/// $(where $($where:where_predicate),* $(,)? )?
/// {
/// $(
/// $(#[$variant_meta:meta])*
/// $variant:ident $(<$($var_gen_args:generic_arg)*>)?
/// // additional bounds for the MakeTypeWitness impl that constructs this variant.
/// $(where $($vari_where:where_predicate)*)?
/// // the type that this variant requires the
/// // implicit `__Wit` type parameter to be.
/// = $witnessed_ty:ty
/// ),*
/// $(,)?
/// }
/// ```
///
/// <span id = "var_gen_args-param"></span> `<$($var_gen_args:generic_arg)*>`
/// (optional parameter)[(example usage)](#var_gen_args-example):
/// this parameter overrides the generic arguments of the enum in its
/// [`MakeTypeWitness`] implementation.
///
/// <span id = "derive"></span>
/// `derive($($derive:ident),* $(,)?)`(optional parameter)[(example)](#derive-example):
/// supports deriving the traits listed in the [derivation](#derivation) section
///
/// `#[cfg(...)]` attributes on variants are copied to their respective
/// [`MakeTypeWitness`] impls.
///
/// Generic parameters support the `#[cfg(...)]` attribute,
/// no other attribute is supported.
///
/// Defaults for generic parameters are only used
/// as the default value of [`$var_gen_args`](#var_gen_args-param)
/// [(example usage)](#var_gen_args-example) .
///
/// <details>
/// <summary>
/// <b>Soft-deprecated older syntax</b>
/// </summary>
///
/// This macro originally required the following syntax,
/// which is soft-deprecated, and will be supported for the rest of `"1.*"` versions.
///
/// ```text
/// $(#[$enum_meta:meta])*
/// // Allows deriving some traits without the bounds that
/// // standard derives add to type parameters.
/// $(derive($($derive:ident),* $(,)?)))?
/// $vis:vis enum $enum:ident $([$($generics:tt)*])?
/// // The where clause of the enum
/// $(where[$($where:tt)*])?
/// {
/// $(
/// $(#[$variant_meta:meta])*
/// $variant:ident $([$($var_gen_args:tt)*])?
/// // additional bounds for the MakeTypeWitness impl that constructs this variant.
/// $(where[$($vari_where:tt)*])?
/// // the type this variant requires the implicit `__Wit` type parameter to be.
/// = $witnessed_ty:ty
/// ),*
/// $(,)?
/// }
/// ```
///
/// </details>
///
/// ### Limitations
///
/// <span id = "const-parameter-limitation"></span>
/// When used in Rust versions prior to 1.59.0,
/// type witnesses declared with this macro cannot have const parameters,
/// because this macro always adds a `__Wit` type parameter after all generic parameters,
/// and those old versions don't allow type parameters after const parameters.
///
/// # Examples
///
/// ### Basic
///
/// This example demonstrates a basic usage of this macro
///
/// ```rust
/// use typewit::MakeTypeWitness;
///
/// assert_eq!(do_it(1), 1);
/// assert_eq!(do_it(2), 4);
/// assert_eq!(do_it(3), 9);
/// assert_eq!(do_it("foo"), 3);
/// assert_eq!(do_it("hello"), 5);
///
/// const fn do_it<'a, T>(arg: T) -> usize
/// where
/// Witness<'a, T>: MakeTypeWitness,
/// {
/// match MakeTypeWitness::MAKE {
/// // `te` is a `TypeEq<T, u8>`, `te.to_right(arg)` goes from `T` to `u8.`
/// Witness::U8(te) => (te.to_right(arg) as usize).pow(2),
///
/// // `te` is a `TypeEq<T, &'a str>`, `te.to_right(arg)` goes from `T` to `&'a str.`
/// Witness::Str(te) => te.to_right(arg).len(),
/// }
/// }
///
/// typewit::simple_type_witness! {
/// // Declares an `enum Witness<'a, __Wit>`,
/// // the `__Wit` type parameter is added after all generics.
/// enum Witness<'a> {
/// // This variant requires `__Wit == u8`
/// U8 = u8,
/// // This variant requires `__Wit == &'a str`
/// Str = &'a str,
/// }
/// }
/// ```
/// the above invocation of `simple_type_witness` effectively generates this code:
/// ```rust
/// enum Witness<'a, __Wit> {
/// U8(typewit::TypeEq<__Wit, u8>),
/// Str(typewit::TypeEq<__Wit, &'a str>),
/// }
/// impl<'a, __Wit> typewit::TypeWitnessTypeArg for Witness<'a, __Wit> {
/// type Arg = __Wit;
/// }
/// impl<'a> typewit::MakeTypeWitness for Witness<'a, u8> {
/// const MAKE: Self = Self::U8(typewit::TypeEq::NEW);
/// }
/// impl<'a> typewit::MakeTypeWitness for Witness<'a, &'a str> {
/// const MAKE: Self = Self::Str(typewit::TypeEq::NEW);
/// }
/// ```
/// (consult the [generated items] section for all the generated impls)
///
/// ### where clauses
///
/// This example demonstrates a variant with a where clause.
/// ```rust
/// # use std::fmt::Debug;
/// typewit::simple_type_witness! {
/// // Declares an `enum Witness<'a, T, __Wit>`,
/// // the `__Wit` type parameter is added after all generics.
/// #[non_exhaustive]
/// enum Witness<'a, T: 'a>
/// where
/// T: 'a + Debug
/// {
/// // This variant requires `__Wit == T`.
/// // The `MakeTypeWitness` impl for this variant also requires `T: Copy`.
/// #[cfg(feature = "foo")]
/// Value where T: Copy = T,
///
/// // This variant requires `__Wit == &'a T`
/// Ref = &'a T,
/// }
/// }
/// ```
/// the above invocation of `simple_type_witness` effectively generates this code:
/// ```rust
/// # use core::fmt::Debug;
/// #
/// #[non_exhaustive]
/// enum Witness<'a, T: 'a, __Wit: ?Sized>
/// where
/// T: 'a + Debug,
/// {
/// #[cfg(feature = "foo")]
/// Value(typewit::TypeEq<__Wit, T>),
///
/// Ref(typewit::TypeEq<__Wit, &'a T>),
/// }
///
/// impl<'a, T: 'a, __Wit: ?Sized> typewit::TypeWitnessTypeArg for Witness<'a, T, __Wit>
/// where
/// T: 'a + Debug,
/// {
/// type Arg = __Wit;
/// }
///
/// #[cfg(feature = "foo")]
/// impl<'a, T: 'a> typewit::MakeTypeWitness for Witness<'a, T, T>
/// where
/// T: 'a + Debug + Copy,
/// {
/// const MAKE: Self = Self::Value(typewit::TypeEq::NEW);
/// }
///
/// impl<'a, T: 'a> typewit::MakeTypeWitness for Witness<'a, T, &'a T>
/// where
/// T: 'a + Debug,
/// {
/// const MAKE: Self = Self::Ref(typewit::TypeEq::NEW);
/// }
/// ```
/// (consult the [generated items] section for all the generated impls)
///
/// <span id = "var_gen_args-example"></span>
/// ### `$var_gen_args` parameter
///
/// This example shows what the `$var_gen_args` parameter does,
/// as well as how generic parameter defaults relate to it.
///
/// ([this example requires Rust 1.59.0](#const-parameter-limitation))
///
#[cfg_attr(not(feature = "rust_1_61"), doc = "```ignore")]
#[cfg_attr(feature = "rust_1_61", doc = "```rust")]
/// typewit::simple_type_witness! {
/// // Declares an `enum Foo<T, const N: usize, __Wit>`,
/// // the `__Wit` type parameter is added after all generics.
/// //
/// // The defaults for generic parameters are only used
/// // as the default value of the generic arguments of variants.
/// enum Foo<T = i8, const N: usize = 1234> {
/// // This variant requires `__Wit == u64`.
/// //
/// // The `<(), 3>` here
/// // replaces `impl<T, const N: usize> MakeTypeWitness for Foo<T, N, u64>`
/// // with `impl MakeTypeWitness for Foo<(), 3, u64>`.
/// // Using `<(), 3>` allows the `T` and `N` type parameters to be inferred
/// // when the `MakeTypeWitness` impl for `Foo<_, _, u64>` is used.
/// U64<(), 3> = u64,
/// // This variant requires `__Wit == bool`.
/// //
/// // The `<>` here uses the defaults for the generic arguments to
/// // replace `impl<T, const N: usize> MakeTypeWitness for Foo<T, N, bool>`
/// // with `impl MakeTypeWitness for Foo<i8, 1234, bool>`.
/// Bool<> = bool,
/// // This variant requires `__Wit == [T; N]`.
/// Array = [T; N],
/// }
/// }
/// ```
/// the above effectively expands to this:
#[cfg_attr(not(feature = "rust_1_61"), doc = "```ignore")]
#[cfg_attr(feature = "rust_1_61", doc = "```rust")]
/// enum Foo<T, const N: usize, __Wit: ?Sized> {
/// U64(typewit::TypeEq<__Wit, u64>),
/// Bool(typewit::TypeEq<__Wit, bool>),
/// Array(typewit::TypeEq<__Wit, [T; N]>),
/// }
/// impl<T, const N: usize, __Wit: ?Sized> typewit::TypeWitnessTypeArg for Foo<T, N, __Wit> {
/// type Arg = __Wit;
/// }
/// impl typewit::MakeTypeWitness for Foo<(), 3, u64> {
/// const MAKE: Self = Self::U64(typewit::TypeEq::NEW);
/// }
/// impl typewit::MakeTypeWitness for Foo<i8, 1234, bool> {
/// const MAKE: Self = Self::Bool(typewit::TypeEq::NEW);
/// }
/// impl<T, const N: usize> typewit::MakeTypeWitness for Foo<T, N, [T; N]> {
/// const MAKE: Self = Self::Array(typewit::TypeEq::NEW);
/// }
/// ```
/// (consult the [generated items] section for all the generated impls)
///
/// <span id = "derive-example"></span>
/// ### Derives
///
/// This example demonstrates derivation of all the supported traits
/// using the `derive(...)` syntax (as opposed to the `#[derive(...)]` attribute).
///
/// ```rust
/// use typewit::{MakeTypeWitness, TypeEq};
///
/// struct NoImpls;
///
/// assert_eq!(Witness::<u8>::MAKE, Witness::<u8>::MAKE);
///
/// // Witness doesn't require its type parameters to impl any traits in its derives.
/// // The standard derives require that type parameters impl the derived trait,
/// // so this comparison wouldn't work (because `NoImpls` doesn't impl `PartialEq`).
/// assert_eq!(Witness::<NoImpls>::MAKE, Witness::NoImp(TypeEq::NEW));
///
/// typewit::simple_type_witness! {
/// // Declares an `enum Witness<__Wit>`,
/// // the `__Wit` type parameter is added after all generics.
/// derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)
/// enum Witness {
/// U8 = u8,
/// NoImp = NoImpls,
/// }
/// }
/// ```
///
///
/// [`TypeEq`]: crate::TypeEq
/// [`TypeWitnessTypeArg`]: crate::TypeWitnessTypeArg
/// [`MakeTypeWitness`]: crate::MakeTypeWitness
/// [generated items]: #generated-items
#[macro_export]
macro_rules! simple_type_witness {
(
$(#[$enum_meta:meta])*
$(derive($($derive:ident),* $(,)?))?
$(pub $(($($pub:tt)*))?)?
enum $enum:ident $($rem:tt)*
) => {
$crate::__parse_generics!{
($crate::__stw_with_parsed_generics!(
(
$(#[$enum_meta])*
derive($($($derive)*)?)
$(pub $(($($pub)*))? )?
enum $enum
)
$enum
))
[$($rem)*]
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_with_parsed_generics {
(
($($prev_args:tt)*)
$enum:ident
[$(($gen_arg:tt ($($gen_phantom:tt)*) $( = $($gen_def:tt)* )? ))*]
[$(($($generics:tt)*))*]
$deleted_markers:tt
$($rem:tt)*
) => {
$crate::__parse_where_clause_for_item! {
($crate::__stw_with_parsed_where ! (
(
$($prev_args)*
[$($($generics)*,)*]
[ $($gen_arg,)* ]
)
[
$enum
[
$((
( $(($($gen_def)*))? ($gen_arg) )
( $(($($gen_def)*))? [$gen_arg] )
))*
]
]
))
$($rem)*
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_with_parsed_where {
(
($($prev_args:tt)*)
$vari_vars:tt
$where_clause:tt
{$($variants:tt)*}
) => {
$crate::__::__stw_parse_variants!{
($($prev_args)* where $where_clause)
$vari_vars
[]
[$($variants)*]
}
}
}
macro_rules! declare__stw_parse_variants {
($_:tt ($($vari_params:tt)*) ($($vari_output:tt)*) $variant_:ident) => {
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_parse_variants_ {
(
($_($fixed:tt)*)
$vari_vars:tt
// fast path for enums with no attributes on variants other than docs
[$_(($variant:ident ($_(#[doc $_($docs:tt)*])*) $_($rem_vari:tt)*))*]
[$_(,)*]
)=>{
$crate::__stw_with_parsed_args! {
$_($fixed)*
{ $_(($variant ($_(#[doc $_($docs)*])*) $_($rem_vari)*))* }
}
};
(
$fixed:tt
$vari_vars:tt
[
($pvariant:ident ($_($pattrs:tt)*) $_($prem:tt)*)
$_($variants:tt)*
]
[$_(,)*]
)=>{
$crate::__stw_parse_variants_attrs!{
$fixed
[/*prev variants*/] [ ($pvariant ($_($pattrs)*) $_($prem)*) $_($variants)*]
[/*prev attrs of variant*/] [/*prev cfgs of variant*/] [$_($pattrs)*]
}
};
(
$fixed:tt
$vari_vars:tt
[$_($prev:tt)*]
[$($vari_params)* = $var_type:ty $_(, $_($rem:tt)*)?]
)=>{
$crate::__::__stw_parse_variants!{
$fixed
$vari_vars
// The four token trees in the parentheses here are:
// (
// variant_name
// ($($attributes:tt)*)
// (replacement_for_Self)
// [where_clause_for_variant]
// withnessed_type
// )
[$_($prev)* ($($vari_output)* () [] $var_type)]
[$_($_($rem)*)?]
}
};
(
$fixed:tt
[$enum:ident $gen_with_defs:tt]
$prev:tt
[$($vari_params)* < $_($rem:tt)*]
)=>{
$crate::__::__parse_generic_args_with_defaults! {
(
(
($crate::__stw_parse_variant_Self_ty!(
$fixed
[$enum $gen_with_defs]
$prev
($($vari_output)*)
))
$crate::__::concat!(
"`",
$crate::__::stringify!($_ $variant_),
"` variant",
)
)
[]
$gen_with_defs
)
[]
[]
[$_($rem)*]
}
};
(
$fixed:tt
[$enum:ident $gen_with_defs:tt]
$prev:tt
[$($vari_params)* [$_($SelfArgs:tt)*] $_($rem:tt)*]
)=>{
$crate::__::__parse_generic_args_with_defaults! {
(
(
($crate::__stw_parse_variant_Self_ty!(
$fixed
[$enum $gen_with_defs]
$prev
($($vari_output)*)
))
$crate::__::concat!(
"`",
$crate::__::stringify!($_ $variant_),
"` variant",
)
)
[]
$gen_with_defs
)
[]
[]
[$_($SelfArgs)* > $_($rem)*]
}
};
($fixed:tt $vari_vars:tt $prev:tt [$($vari_params)* where $_($rem:tt)*])=>{
$crate::__parse_where_clause_for_item!{
($crate::__stw_parsed_variant_with_where_clause!(
$fixed $vari_vars $prev [$($vari_output)* ()]
))
where $_($rem)*
}
};
}
};
}
declare__stw_parse_variants!{
$
($(#[$($vari_attr:tt)*])* $variant:ident)
($variant ($(#[$($vari_attr)*])*))
variant
}
pub use __stw_parse_variants_ as __stw_parse_variants;
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_parse_variants_attrs {
(
($($fixed:tt)*)
[$($variants:tt)*] []
[] [] []
)=>{
$crate::__stw_with_parsed_args! {
$($fixed)*
{ $($variants)* }
}
};
(
$fixed:tt
$prev_vari:tt $next_vari:tt
$prev_attrs:tt [$($prev_cfgs:tt)*] [#[cfg($($cfg:tt)*)] $($rest_attrs:tt)*]
) => {
$crate::__stw_parse_variants_attrs! {
$fixed
$prev_vari $next_vari
$prev_attrs [$($prev_cfgs)* ($($cfg)*)] [$($rest_attrs)*]
}
};
(
$fixed:tt
$prev_vari:tt $next_vari:tt
[$($prev_attrs:tt)*] $prev_cfgs:tt [#[$($attr:tt)*] $($rest_attrs:tt)*]
) => {
$crate::__stw_parse_variants_attrs! {
$fixed
$prev_vari $next_vari
[$($prev_attrs)* #[$($attr)*]] $prev_cfgs [$($rest_attrs)*]
}
};
// none of the attributes of the variant were cfg
(
$fixed:tt
[$($prev_vari:tt)*]
[
$curr_vari:tt
$(
($nvariant:ident ($($nattrs:tt)*) $($nrem:tt)*)
$($rem_vari:tt)*
)?
]
$prev_attrs:tt [] []
) => {
$crate::__stw_parse_variants_attrs! {
$fixed
[$($prev_vari)* $curr_vari] [
$(($nvariant ($($nattrs)*) $($nrem)*) $($rem_vari)*)?
]
[] [] [$($($nattrs)*)?]
}
};
// some of the attributes of the variant were cfg
(
$fixed:tt
[$($prev_vari:tt)*]
[
($cvariant:ident $__cattrs:tt $($crem:tt)*)
$(
($nvariant:ident ($($nattrs:tt)*) $($nrem:tt)*)
$($rem_vari:tt)*
)?
]
[$($prev_attrs:tt)*] [$(($($cfg:tt)*))+] []
) => {
#[cfg(all($($($cfg)*)+))]
$crate::__stw_parse_variants_attrs! {
$fixed
[$($prev_vari)* ($cvariant ($($prev_attrs)*) $($crem)*) ] [
$(($nvariant ($($nattrs)*) $($nrem)*) $($rem_vari)*)?
]
[] [] [$($($nattrs)*)?]
}
#[cfg(not(all($($($cfg)*)+)))]
$crate::__stw_parse_variants_attrs! {
$fixed
[$($prev_vari)*] [
$(($nvariant ($($nattrs)*) $($nrem)*) $($rem_vari)*)?
]
[] [] [$($($nattrs)*)?]
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_parse_variant_Self_ty {
(
$fixed:tt
[$enum:ident $($rem_vars:tt)*]
$prev:tt
($($vari_prev:tt)*)
[$($SelfTy:tt)*]
where $($rem:tt)*
)=>{
$crate::__parse_where_clause_for_item!{
($crate::__stw_parsed_variant_with_where_clause!(
$fixed
[$enum $($rem_vars)*]
$prev
[$($vari_prev)* ($enum < $($SelfTy)*)]
))
where $($rem)*
}
};
(
$fixed:tt
[$enum:ident $($rem_vars:tt)*]
[$($prev:tt)*]
($($vari_prev:tt)*)
[$($SelfTy:tt)*]
= $var_type:ty
$(, $($rem:tt)*)?
)=>{
$crate::__::__stw_parse_variants!{
$fixed
[$enum $($rem_vars)*]
[$($prev)* ($($vari_prev)* ($enum < $($SelfTy)*) [] $var_type)]
[$($($rem)*)?]
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_parsed_variant_with_where_clause {
(
$fixed:tt
$vari_vars:tt
[$($prev:tt)*]
[$($vari_prev:tt)*]
$where_clause:tt
= $var_type:ty $(, $($rem:tt)*)?
) => {
$crate::__::__stw_parse_variants!{
$fixed
$vari_vars
[$($prev)* ($($vari_prev)* $where_clause $var_type)]
[$($($rem)*)?]
}
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_with_parsed_args {
(
$(# $enum_meta:tt)*
derive $derive:tt
$vis:vis enum $enum:ident $generics:tt $gen_args:tt
where $where:tt
{ $($variant_args:tt)* }
) => {
$crate::__stw_top_items!{
$(# $enum_meta)*
$vis enum $enum $generics $gen_args
where $where
{ $($variant_args)* }
}
$crate::__stw_derive_dispatcher!{
derive $derive
enum $enum $generics $gen_args
where $where
{ $($variant_args)* }
}
$(
$crate::__stw_make_type_witness_impl!{
$enum $generics $gen_args
where $where
$variant_args
}
)*
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_top_items {
(
$(# $enum_meta:tt)*
$vis:vis enum $enum:ident[$($generics:tt)*] [$($gen_args:tt)*]
where [$($where:tt)*]
{
$((
$variant:ident
($(#[$variant_meta:meta])*)
($($SelfTy:tt)*)
$vari_where:tt
$witnessed_ty:ty
))*
}
) => {
$(#$enum_meta)*
$vis enum $enum <$($generics)* __Wit: ?Sized>
where $($where)*
{
$(
$(#[$variant_meta])*
$variant($crate::TypeEq<__Wit, $witnessed_ty>),
)*
}
impl<$($generics)* __Wit: ?Sized> $crate::__::Copy for $enum<$($gen_args)* __Wit>
where $($where)*
{}
impl<$($generics)* __Wit: ?Sized> $crate::__::Clone for $enum<$($gen_args)* __Wit>
where $($where)*
{
fn clone(&self) -> Self {
*self
}
}
impl<$($generics)* __Wit: ?Sized>
$crate::TypeWitnessTypeArg
for $enum<$($gen_args)* __Wit>
where $($where)*
{
type Arg = __Wit;
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_derive_dispatcher {
(
derive($($trait:ident)*)
enum $enum:ident $generics:tt $gen_args:tt
where $where:tt
$variant_args:tt
) => {
$(
$crate::__stw_derive_dispatcher_inner!{
$trait
$enum $generics $gen_args
where $where
$variant_args
}
)*
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_derive_dispatcher_inner {
(
$trait:ident $enum:ident[$($generics:tt)*] [$($gen_args:tt)*]
where [$($where:tt)*]
{$(($variant:ident $($rem:tt)*))*}
) => {
$crate::__stw_single_derive!{
(
impl<$($generics)* __Wit: ?Sized> $enum<$($gen_args)* __Wit>
where
$($where)*
)
(
impl<$($generics)* __Wit: ?Sized>
$crate::__::$trait
for $enum<$($gen_args)* __Wit>
where
$($where)*
)
$trait
[$($variant)*]
}
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_single_derive {
($inh_header:tt ($($impl_header:tt)*) Debug [$($variant:ident)*]) => {
$($impl_header)*
{
fn fmt(&self, f: &mut $crate::__::Formatter<'_>) -> $crate::__::FmtResult {
f.write_str(match self {
$(Self::$variant{..} => stringify!($variant),)*
})
}
}
};
($inh_header:tt ($($impl_header:tt)*) PartialEq [$($variant:ident)*]) => {
$($impl_header)*
{
fn eq(&self, other: &Self) -> $crate::__::bool {
$crate::__::discriminant(self) == $crate::__::discriminant(other)
}
}
};
(($($inh_header:tt)*) ($($impl_header:tt)*) PartialOrd [$($variant:ident)*]) => {
$($inh_header)* {
const fn __variant_number(&self) -> usize {
mod number {
enum __Number__ {
$($variant,)*
}
$( pub const $variant: $crate::__::usize = __Number__::$variant as _; )*
}
match self {
$(Self::$variant{..} => number::$variant,)*
}
}
}
$($impl_header)* {
fn partial_cmp(&self, other: &Self) -> $crate::__::Option<$crate::__::Ordering> {
$crate::__::PartialOrd::partial_cmp(
&self.__variant_number(),
&other.__variant_number(),
)
}
}
};
($inh_header:tt ($($impl_header:tt)*) Ord [$($variant:ident)*]) => {
$($impl_header)* {
fn cmp(&self, other: &Self) -> $crate::__::Ordering {
$crate::__::Ord::cmp(
&self.__variant_number(),
&other.__variant_number(),
)
}
}
};
($inh_header:tt ($($impl_header:tt)*) Eq [$($variant:ident)*]) => {
$($impl_header)* { }
};
($inh_header:tt ($($impl_header:tt)*) Hash [$($variant:ident)*]) => {
$($impl_header)* {
fn hash<H: $crate::__::Hasher>(&self, state: &mut H) {
$crate::__::Hash::hash(&$crate::__::discriminant(self), state)
}
}
};
// this is always implemented
($inh_header:tt $impl_header:tt Copy $variants:tt) => {};
// this is always implemented
($inh_header:tt $impl_header:tt Clone $variants:tt) => {};
($inh_header:tt $impl_header:tt $derive:ident $variants:tt) => {
$crate::__::compile_error!{$crate::__::concat!{
"The `simple_type_witness` macro does not support deriving `",
$crate::__::stringify!($derive),
"`.\n",
"help: You could try using `#[derive(",
$crate::__::stringify!($derive),
")]`.",
}}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __stw_make_type_witness_impl {
(
$enum:ident[$($generics:tt)*] [$($gen_args:tt)*]
where [$($where:tt)*]
(
$variant:ident
$attrs:tt
($( $($SelfTy:tt)+ )?)
[$($vari_where:tt)*]
$witnessed_ty:ty
)
) => {
$crate::__impl_with_span! {
$variant // span
() // attributes on impl block
( <$($generics)* __Wit: ?Sized> $crate::MakeTypeWitness )
// for
(
$crate::__first_ty!(
$($($SelfTy)+ $witnessed_ty>,)?
$enum<$($gen_args)* $witnessed_ty>,
)
)
(
where
Self: $crate::__::Identity<Type = $enum<$($gen_args)* __Wit>>,
$($where)*
$($vari_where)*
)
(
const MAKE: Self = Self::$variant($crate::TypeEq::NEW);
)
}
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __first_ty {
($first:ty, $($rem:tt)*) => {
$first
}
}