dtolnay / quote

Rust quasi-quoting
Apache License 2.0
1.32k stars 90 forks source link

Feature Request : Option Expansion #285

Closed NovaliX-Dev closed 3 days ago

NovaliX-Dev commented 3 days ago

This issue is kinda similar to #20, but it propose something more featured and more general.

The macros in rust have a syntax for tokens with zero or one ocurences : $(<something>)?.

I would like to have this implemented for the quote macro as well, and one of the possible application for this syntax is for the optioned tokens.

Here is an example (assembled in like 5 minutes) of how i'm seeing things :

For the moment I'm implementing this in the manual way, by storing the conditionally produced tokens an intermediate variable and then using that variable in a final quote macro:

let var = Some(quote!{ something })

// ...

let default_tokens = match var {
    Some(val) => quote! { _ => #val },
    None => TokenStream::new()
};

// ...

quote! { bla #default_tokens bla }

The end goal would be to remove the temp variable completely.

What do you all think about this proposal ?

dtolnay commented 3 days ago

This is already supported well enough by interpolating iterators.

let val = option.iter();
quote!(bla #(_ => #val)* bla)
NovaliX-Dev commented 3 days ago

Thank you for your quick response ! But the problem still remains : we create a temp variable, something i was trying to avoid. (Sorry I've didn't mentioned that goal directly).