Questions tagged [rust-macros]

Rust macros are a powerful tool to map a certain input sequence to an output sequence according to a defined procedure.

There are currently two types of macros in Rust:

310 questions
4
votes
1 answer

Parsing content of brackets in rust proc_macro

I am building a small HTML parser in Rust using syn and proc_macro2. I have made it so far that i can parse regular HTML tags and it's attributes So for example html!(
) Works But i would like to parse some JSX…
Tonis
  • 111
  • 5
4
votes
2 answers

How can I emit an integer literal with a non-decimal base using the quote crate?

I'm using quote to generate code to decode assembly operations. The instruction manual for my chip uses binary values to describe the operations, so I'd like my generated code to also express literals as binary values to make it easier for me to…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
4
votes
0 answers

How to debug inside Rust macros

I'm trying to debug an issue in Serde, and I can step through my code fine, but unfortunately Serde is like 90% macros, so it seems like the entire call to deserialize() gets mapped to the line containing #[derive(Deserialize)] and I can't set…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
4
votes
2 answers

How to concatenate token right after a variable within `quote!` macro?

use quote::quote; fn main() { let name = "foo"; let res = quote!(#name bar); println!("{:?}", res.to_string()); } The above code prints "\"foo\" bar". Please try running it here on Rust Playground. How to adjust it to make the single…
0x00A5
  • 1,462
  • 1
  • 16
  • 20
4
votes
2 answers

Can I 'enumerate' with Rust's variadic macros?

Essentially I have a macro that looks like: macro_rules! my_macro { ( $expr:expr; $( $pat:pat ),* ) => { match $expr { $( $pat => $(some-macro-magic-here), )* } } } Is there anything that can go into…
math4tots
  • 8,540
  • 14
  • 58
  • 95
4
votes
1 answer

In a procedural macro, how can I check if a string is a valid variable name and not a keyword?

In a procedural macro, I wish to be able to check a string is a valid variable name and is not a keyword. proc_macro2::Ident will panic if one tries to use an invalid variable name, but it will allow keywords which I do not want to be allowed. It…
Henry Gomersall
  • 8,434
  • 3
  • 31
  • 54
4
votes
1 answer

Infer the name of the calling crate to populate a doctest in a procedural macro

I'm creating a procedural macro that auto-generates a library from some configuration file (it's a register layout, but that's not important for the question). I would like the library to auto-generate the documentation accompanying the auto-library…
Henry Gomersall
  • 8,434
  • 3
  • 31
  • 54
4
votes
1 answer

How to return a new String from a declarative macro?

So here I am, trucking along with Rustlings, until I get broadsided with test 4. It wants me to write a macro that will satisfy the following code: fn main() { if my_macro!("world!") != "Hello world!" { panic!("Oh no! Wrong output!"); …
AerosolSP
  • 177
  • 1
  • 1
  • 10
4
votes
1 answer

Can I repeat matching by several rules in a macro?

Can I repeat match in a Rust macro? I want to be able to do something like: my_dsl! { foo ; bar ; foo ; ... } Basically, an arbitrary number of semicolon-delimited statement, and each…
rincewind
  • 1,103
  • 8
  • 29
4
votes
1 answer

Is it possible to have one crate with both procedural macros and logic?

When you want to share a library that uses a procedural macro, is the dual crate approach foo/foo_derive inevitable? I would like to provide a crate that has the logic and the macros. The first thing I tried was: my_proc_macro ├── Cargo.toml ├──…
Boiethios
  • 38,438
  • 19
  • 134
  • 183
4
votes
1 answer

How to automatically generate incrementing number identifiers for each implementation of a trait?

I have a Component trait that has a method to return an index, like so: trait Component { fn index(&self) -> usize; } These indexes are used for setting flags in a bitset. For example, a Component trait object returning an index of 5 would…
manabreak
  • 5,415
  • 7
  • 39
  • 96
4
votes
2 answers

"expected identifier" when creating a macro to define mutable variables at the same time

I want to declare multiple mutable variables at the same time. Defined a macro to declare mutable variables as the following. macro_rules! mutf64 { ( $( $e:expr ),+ ) => { { $( let mut $e:f64; …
madeinQuant
  • 1,721
  • 1
  • 18
  • 29
4
votes
1 answer

Is it possible to use an item arg passed to a macro as a method?

I'm trying to create a macro that generates a struct that provides a set of methods that are passed into the macro. For example, calling: create_impl!(StructName, fn foo() -> u32 { return 432 }) should generate an empty struct StructName that…
Donald Whyte
  • 195
  • 2
  • 9
3
votes
2 answers

Why is "-x" trying to parse as a literal and failing in declarative macros?

I have a little macro I wrote to help me explore what things are parsed as. With a few arms lopped off, it looks like: macro_rules! show_me{ ($l : literal $($tail : tt)*) => { println!("{} is a literal and then I see {}", stringify!($l),…
Edward Peters
  • 3,623
  • 2
  • 16
  • 39
3
votes
1 answer

How can a macro match any token tree except comma

macro_rules! foo { ( $( $( $tok:tt )* ,)* ) => { [ bar!( $( $tok )* ,)* ] } } Produces error: local ambiguity when calling macro 'foo'. Because the comma ',' is of type :tt. I would need a way to match all token trees excluding the…
uben
  • 1,221
  • 2
  • 11
  • 20