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.
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.
Macros by example (macro_rules!) can’t create new identifiers; they can only use ones that they are given. So get_$name isn’t going to work without some external help. You can either use a crate like paste that provides this ability or restructure the code you’re producing. For example, something like this has similar ergonomics and can be easily produced via macro:
The difference being that i! is meant for use in impl and includes pub, while ti! is for traits and can't. Is there any way to conditionally include the pub qualifier somehow? No biggy if not--that's the only property macro I need to implement for traits, so if I need to preserve that duplication then that's fine. I'm just working with an API that's in lots of flux, so the more boilerplate I can generate, the better off I am.