3 ms·
I think you can do something like this with your Rust code using macros. struct P0{} impl P0{ fn write(self:&Self,pin:usize){ std::println!(
by jbandela1 6y ago
I think you can do something like this with your Rust code using macros.
struct P0{}
impl P0{
fn write(self:&Self,pin:usize){
std::println!("Writing port P0 on pin {}",pin);
}
}
struct P1{}
impl P1{
fn write(self:&Self,pin:usize){
std::println!("Writing port P1 on pin {}",pin);
}
}
macro_rules! for_each_port_pin{
($port:ident,$pin:ident,$b:block,
$(($e1:expr,$e2:expr)),*) =>{
$(
let $port = $e1;
let $pin = $e2;
$b
);*
}
}
fn main(){
let p0 = P0{};
let p1 = P1{};
for_each_port_pin!(port,pin,
{port.write(pin);},
(&p0,10usize),(&p1,7usize)
);
}
Rust playground link:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e1d2ad80349ff5c3f49ab8330e7db918 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- afranchuk 6y agoOr a trait...
- alfiedotwtf 6y agoYeah, I was thinking trait while reading that bit... maybe I missed something?