Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #[warn(dead_code)]
  2. #[derive(Debug)]
  3. enum Gpio {
  4. Bcm1,
  5. Bcm2,
  6. Bcm3,
  7. Bcm4
  8. }
  9.  
  10. struct Ports {
  11. bcm1: Gpio,
  12. bcm2: Gpio,
  13. bcm3: Gpio,
  14. bcm4: Gpio,
  15. }
  16.  
  17. macro_rules! foo {
  18. () => (());
  19. }
  20.  
  21. macro_rules! port_name {
  22. ( &ports.$name:ident ) => ( $name );
  23. ( ports.$name:ident ) => ( $name );
  24. }
  25.  
  26. macro_rules! port_type {
  27. ( (&ports.)$name:ident ) => ( &'a Gpio );
  28. ( (ports.)$name:ident ) => ( Gpio );
  29. }
  30.  
  31. macro_rules! port_value {
  32. ( &ports.$name:ident ) => ( &'a ports.$name );
  33. ( ports.$name:ident ) => ( ports.$name );
  34. }
  35.  
  36. macro_rules! fields {
  37. () => (());
  38. (&ports.$name:ident, $($tail:tt)*) => ($name: &'a Gpio, fields!(tail););
  39. (ports.$name:ident, $($tail:tt)*) => ($name: Gpio, fields!(tail););
  40. }
  41.  
  42. macro_rules! st {
  43. ($name:ident, $fields:expr) => (struct $name<'a> { $fields })
  44. }
  45.  
  46. macro_rules! configure {
  47. ( $peripheral:ident: $($ports:tt)* ) => {
  48. #[derive(Debug)]
  49. st!($peripheral, fields!($($ports)*));
  50. //let $peripheral = $peripheral { $( a: port_value!($port)),* };
  51. };
  52. }
  53.  
  54. fn main() {
  55. let ports = Ports {bcm1: Gpio::Bcm1, bcm2: Gpio::Bcm2, bcm3: Gpio::Bcm3, bcm4: Gpio::Bcm4};
  56. //let a = ports.bcm1;
  57. //let b = ports.bcm2;
  58. configure!(motor1: &ports.bcm1, ports.bcm2);
  59. //struct Foo {fields!(&ports.bcm1, ports.bcm2);};
  60. //configure!(motor2: &ports.bcm1, ports.bcm3);
  61. /*
  62. #[derive(Debug)]
  63. struct Peripheral<'a> {
  64. port1: &'a Gpio,
  65. port2: Gpio
  66. }
  67. let motor1 = Peripheral { port1: &ports.bcm1, port2: ports.bcm2 };
  68. let motor2 = Peripheral { port1: &ports.bcm1, port2: ports.bcm3 };
  69. */
  70. println!("motor1:{:?}", motor1);
  71. println!("motor2:{:?}", motor2);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement