Questions tagged [macro-rules]

20 questions
0
votes
1 answer

How to parse nested optional tokens in macro_rules?

When trying to do macro_rules! tipey { (Vec<$pt: tt>) => { 2 }; (Vec>) => { 3 }; ($pt: tt) => { 1 }; } macro_rules! structy { (struct $i: ident { $($p: ident: $pt: tt $(<$ppt: tt $(<$gt: tt> )? > )?),+ …
ditoslav
  • 4,563
  • 10
  • 47
  • 79
0
votes
1 answer

How to match a struct instantiation with macro_rules

Since this took me a while to figure out, I may as well share how I fixed it. I was trying to wrap every item on a struct with some function, in my case Arc::new(Mutex::new(item)) with macro_rules My initial attempt was many variations on…
Luctins
  • 77
  • 1
  • 6
0
votes
1 answer

Macros, How to generate enum variant from str literal?

I have an enum with over 100 variants. and I have to get each of its variants from a string. Something like this. enum VeryLongEnum { A, B, C, D, E, } impl From<&'static str > for VeryLongEnum { fn from(s: &'static str) ->…
al3x
  • 589
  • 1
  • 4
  • 16
0
votes
2 answers

Macro that implements functions based on type

How can I write a macro that implements the same methods for multiple structs? The methods should be slightly different based on the struct. The code should look something like this: macro_rules! say_name { (for $($t:ty),+) => { $(impl…
Jad Haddad
  • 71
  • 7
0
votes
1 answer

Rust macro to format arguments over multiple formats

TL;DR I'm trying to write a macro that will do the following transformation: magic_formatter!(["_{}", "{}_", "_{}_"], "foo") == [format!("_{}", "foo"), format!("{}_", "foo"), format!("_{}_", "foo")] (a solution that will give…
NivPgir
  • 63
  • 4
1
2