Hey folks. I'm working on a library with lots of functions that look like:
pub fn get_noise_type(&self) -> Result<i32, SynthizerError> {
self.handle().get_i(Property::NoiseType.to_i32().unwrap())
}
pub fn set_noise_type(&self, value: i32) -> Result<(), SynthizerError> {
self.handle()
.set_i(Property::NoiseType.to_i32().unwrap(), value)
}
If possible, I'd like to generate that code via macro, something like:
i!(noise_type, Property::NoiseType);
To be clear, this is an internal API, and the end goal is to generate code identical to the above. I just have a number of different property types, some of which perform some hairy and unsafe operations, so really want this code generated once and only once.
I have this macro:
macro_rules! i {
($name:tt, $property:path) => {
pub fn get_$name(&self) -> Result<i32, SynthizerError> {
self.handle().get_i($property.to_i32().unwrap())
}
pub fn set_$name(&self, value: i32) -> Result<(), SynthizerError> {
self.handle().set_i($property.to_i32().unwrap(), value)
}
};
}
This reports:
error: expected one of `(` or `<`, found `noise_type`
--> synthizer-rs\src\lib.rs:435:20
|
435 | pub fn get_$name(&self) -> Result<i32, SynthizerError> {
| ^^^^^ expected one of `(` or `<`
Is what I'm attempting even possible? If so, how do I make it work? I tried $name
as an ident
as well but it gave the same result.
Thanks.