Guest User

Untitled

a guest
Nov 17th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. // So I want a macro that will generate this kind of construction:
  2. //
  3. // #[repr(u8)]
  4. // #[derive(Clone, Copy, Debug)]
  5. // #[allow(dead_code)]
  6. // pub enum CommonCommand {
  7. // SelectFile = 0xa4,
  8. // GetResponse = 0xc0,
  9. // }
  10. //
  11. // impl Into<u8> for CommonCommand {
  12. // fn into(&self) -> u8 {
  13. // (*self) as u8
  14. // }
  15. // }
  16. //
  17. // from something like this (happy to change the syntax):
  18. //
  19. // command_type! {
  20. // CommonCommand {
  21. // SelectFile = 0xa4,
  22. // GetResponse = 0xc0,
  23. // }
  24. // }
  25.  
  26. macro_rules! command_type {
  27. ( $name:ident { $($val:expr),+ } ) => {{
  28. #[repr(u8)]
  29. #[derive(Clone, Copy, Debug)]
  30. #[allow(dead_code)]
  31. pub enum $name {
  32. $($val),+
  33. }
  34.  
  35. impl Into<u8> for $name {
  36. fn into(&self) -> u8 {
  37. (*self) as u8
  38. }
  39. }
  40. }}
  41. }
  42.  
  43. command_type! {
  44. CommonCommand {
  45. SelectFile = 0xa4,
  46. GetResponse = 0xc0,
  47. }
  48. }
Add Comment
Please, Sign In to add comment