Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. macro_rules! derive_match {
  2. (@with_dollar![$dol:tt]
  3. $matching_Enum:ident in [$($absolute_path:tt)*],
  4. $( #[$($meta:meta)*] )*
  5. $pub:vis
  6. enum $Enum:ident {
  7. $(
  8. $( #[$($variant_meta:meta)*] )*
  9. $VariantN:ident $value:tt,
  10. )*
  11. }
  12. ) => (
  13. $( #[$($meta)*] )*
  14. $pub
  15. enum $Enum {
  16. $(
  17. $( #[$($variant_meta)*] )*
  18. $VariantN $value,
  19. )*
  20. }
  21.  
  22. macro_rules! $matching_Enum {(
  23. let $dol Variant:tt $dol pat:tt = $dol expr:expr;
  24. $dol($dol body:tt)*
  25. ) => ({
  26. #[allow(unused_imports)]
  27. use $($absolute_path)*::$Enum::*;
  28. match $dol expr {
  29. $(
  30. $VariantN $dol pat => {
  31. $dol($dol body)*
  32. },
  33. )*
  34. }
  35. })}
  36. );
  37.  
  38. (
  39. $matching_Enum:ident in [$($absolute_path:tt)*],
  40. $( #[$($meta:meta)*] )*
  41. $pub:vis
  42. enum $Enum:ident {
  43. $(
  44. $( #[$($variant_meta:meta)*] )*
  45. $VariantN:ident $value:tt
  46. ),* $(,)?
  47. }
  48. ) => (derive_match!{@with_dollar![$]
  49. $matching_Enum in [$($absolute_path)*],
  50. $( #[$($meta)*] )*
  51. $pub
  52. enum $Enum {
  53. $(
  54. $( #[$($variant_meta)*] )*
  55. $VariantN $value,
  56. )*
  57. }
  58. });
  59. }
  60.  
  61. derive_match!{
  62. matching_Foo in [crate],
  63. enum Foo {
  64. Foo1(Bar<u8>),
  65. Foo2(Bar<u32>),
  66. }
  67. }
  68.  
  69. struct Bar<U> (U);
  70.  
  71. impl<U> Bar<U> {
  72. fn bar (self: &'_ Self, word: &'_ str)
  73. {}
  74. }
  75.  
  76. fn foo (something: Foo) {
  77. let a_word = "A word";
  78. matching_Foo!{
  79. let FooN(inner) = something;
  80. inner.bar(a_word);
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement