Hi Folks,
I am coming from the C/C++ languages and I know the macro system in rust is very powerful because it gives alot to programmers in terms of of things like fancy tokenizing and of course the decorative style of macros.
I am trying to do something, simple and lot of example focus on the more advanced features. I am doing something very simple, and haven found examples to help out on just basic macros; lets not focus on if this macro can be done in none macro way, I am just trying this out to get a little bit familiar with how macros are done:
macro_rules! TEST {
( $a:ident, $b:ident, $v:expr ) => {
self.reg_map.insert(CPURegs::$a, ($v & 0xFF00) >> 8);
self.reg_map.insert(CPURegs::$b ,$v & 0x00FF);
};
}
So here CPURegs is an enum, and I have a few places where I do this with different enum values, so I thought why not write a macro just to generate some code do these inserts, by taken he macro value name and the value to insert into a struct member hashmap. Where I am using the macro self, should be in scope:
fn set_reg(&mut self, dest: CPURegs, val: u16) {
TEST!(H,L, val);
}
And I get: ^^^^ self
value is a keyword only available in methods with a self
parameter , as the error. Also not sure of ident and expr are the right token type here, but its what I I wanted to try with.
Thanks Again!