Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[warn(dead_code)]
- #[derive(Debug)]
- enum Gpio {
- Bcm1,
- Bcm2,
- Bcm3,
- Bcm4
- }
- struct Ports {
- bcm1: Gpio,
- bcm2: Gpio,
- bcm3: Gpio,
- bcm4: Gpio,
- }
- macro_rules! foo {
- () => (());
- }
- macro_rules! port_name {
- ( &ports.$name:ident ) => ( $name );
- ( ports.$name:ident ) => ( $name );
- }
- macro_rules! port_type {
- ( (&ports.)$name:ident ) => ( &'a Gpio );
- ( (ports.)$name:ident ) => ( Gpio );
- }
- macro_rules! port_value {
- ( &ports.$name:ident ) => ( &'a ports.$name );
- ( ports.$name:ident ) => ( ports.$name );
- }
- macro_rules! fields {
- () => (());
- (&ports.$name:ident, $($tail:tt)*) => ($name: &'a Gpio, fields!(tail););
- (ports.$name:ident, $($tail:tt)*) => ($name: Gpio, fields!(tail););
- }
- macro_rules! st {
- ($name:ident, $fields:expr) => (struct $name<'a> { $fields })
- }
- macro_rules! configure {
- ( $peripheral:ident: $($ports:tt)* ) => {
- #[derive(Debug)]
- st!($peripheral, fields!($($ports)*));
- //let $peripheral = $peripheral { $( a: port_value!($port)),* };
- };
- }
- fn main() {
- let ports = Ports {bcm1: Gpio::Bcm1, bcm2: Gpio::Bcm2, bcm3: Gpio::Bcm3, bcm4: Gpio::Bcm4};
- //let a = ports.bcm1;
- //let b = ports.bcm2;
- configure!(motor1: &ports.bcm1, ports.bcm2);
- //struct Foo {fields!(&ports.bcm1, ports.bcm2);};
- //configure!(motor2: &ports.bcm1, ports.bcm3);
- /*
- #[derive(Debug)]
- struct Peripheral<'a> {
- port1: &'a Gpio,
- port2: Gpio
- }
- let motor1 = Peripheral { port1: &ports.bcm1, port2: ports.bcm2 };
- let motor2 = Peripheral { port1: &ports.bcm1, port2: ports.bcm3 };
- */
- println!("motor1:{:?}", motor1);
- println!("motor2:{:?}", motor2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement