Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #[macro_export]
  2. macro_rules! assert_variant {
  3. ($e:path: $($v:ident),+ $(,)?) => {
  4. #[allow(unused_imports)]
  5. const _: fn() = || {
  6. use $e as AnEnum;
  7. $(
  8. use AnEnum::$v as _;
  9. )+
  10. };
  11. };
  12. }
  13.  
  14. pub mod tests {
  15.  
  16. #[allow(dead_code)]
  17. pub enum Thing {
  18. A,
  19. B { x: () },
  20. C(()),
  21. AnEnum,
  22. }
  23.  
  24. #[test]
  25. fn should_compile() {
  26. assert_variant!(Thing: A);
  27. assert_variant!(Thing: B);
  28. assert_variant!(Thing: C);
  29.  
  30. assert_variant!(self::Thing: AnEnum, A, B, C);
  31. }
  32.  
  33. /// # Single missing variant
  34. ///
  35. /// ```fail_compile
  36. /// use playground::assert_variant;
  37. /// use playground::tests::Thing;
  38. ///
  39. /// assert_variant!(Thing: D);
  40. /// ```
  41. ///
  42. /// # Multiple missing variants
  43. ///
  44. /// ```fail_compile
  45. /// use playground::assert_variant;
  46. /// use playground::tests::Thing;
  47. ///
  48. /// assert_variant!(Thing: D, E, F);
  49. /// ```
  50. ///
  51. /// # Multiple missing variants with a single existing variant.
  52. ///
  53. /// ```fail_compile
  54. /// use playground::assert_variant;
  55. /// use playground::tests::Thing;
  56. ///
  57. /// assert_variant!(Thing: A, D, E, F);
  58. /// ```
  59. ///
  60. /// # Missing variant with path to Enum
  61. ///
  62. /// ```fail_compile
  63. /// use playground::assert_variant;
  64. /// use playground::tests::Thing;
  65. ///
  66. /// assert_variant!(playground::tests::Thing: D, AnEnum);
  67. /// ```
  68. #[allow(dead_code)]
  69. fn fail_compile() {}
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement