macro_rules! demo {
($name:expr) => {
};
($name:expr) => {
};
}
Can I match the name based on the data type?
macro_rules! demo {
($name:expr) => {
};
($name:expr) => {
};
}
Can I match the name based on the data type?
No; Rust macros get expanded before any type analysis happens, so that information isn’t available to them.
You might be able to get something working with macros + generics/traits though.
Thank you for your answer.
Can you be more detailed?
First, can you be more detailed? Please elaborate on what problem are you trying to solve, and show some of your relevant code.
thanks for your answer.
The root problem is as follow:
In C, registers are defined and passed in in different ways.
#define register_1 x1
#define register_2 x2
#define str(x) #x
#define write(name) asm ("msr " str(name)", xwz"));
u32 x3;
write(register_1) -> asm ("msr x1, xwz);
write(register_2) ->asm ("msr x2, xwz);
write(x3) ->asm ("msr x3, xwz);
write(el2) ->asm ("msr el2, xwz);
In rust, what should I do
I'm not familiar with Rust inline assembly. It uses a macro and Rust macros are fairly different from C macros, so it might not be possible (at least without procedural macros). Can you show an example of what you want your Rust output to look like?
In rust, I still want to define a macro , the parameters can be any of the above types, and the output is correct.
for example:
macro_rules! write {
($name:expr) => {
unsafe {
asm!(concat!("msr ", stringify!($name), ", xzr"));
}
};
}
const register_1: &str = "x1";
const register_2: &str = "x2";
const x3: u32 = 0;
write!(register_1) -> asm("msr register_1, xwz); //This result is not what I wanted.
write!(register_2) ->asm("msr register_1, xwz);//This result is not what I wanted.
write!(x3) ->asm ("msr x3, xwz);
write!(el2) ->asm ("msr el2, xwz);
Macros can't use anything that's not passed into them as tokens, they are expanded earlier than the constant resolution, for example. What is the business goal you're trying to achieve? What should this code do?