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
2
votes
0 answers

Design choice of default trait implementation vs. derive macro in Rust

After reading quite a bit I haven't found much in the way how to decide whether to implement functionality as a trait with a default implementation or as a derive macro. It appears that the functionality of both is quite similar in many situations,…
bicarlsen
  • 1,241
  • 10
  • 27
2
votes
0 answers

Rust macro for creating new struct from struct

Is there any way to create a macro in rust that returns a new struct with the fields that I want. I have been working on this for weeks and cannot find a suitable solution. Seeing the problem below, is this even possible? And if so, how would I go…
Big G
  • 211
  • 1
  • 9
2
votes
1 answer

How should I use `macro_export` for a custom module

I have the following structure: Inside queues.rs I have a #[macro_export], #[macro_export] macro_rules! queue {...} but I am not sure how to import it in lib.rs. What I have looks like this: use crate::utils::queues::*; // use it here let mut…
dearn44
  • 3,198
  • 4
  • 30
  • 63
2
votes
1 answer

How can I match a literal value in a macro called from another macro?

How can I pass true or false through a macro and into another macro, where the inner macro matches on true or false? Not expr, but literally true or false. When I do the following, I can't find the correct syntax to match against the boolean…
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
2
votes
1 answer

How to expand macros in `tests` folder

How to use cargo expand to expand macros on test files under the tests folder in a crate? crate - src - lib.rs - tests - my_test.rs Cargo.toml cargo expand --lib --tests do not recognize them(?). --bin doesn't recognize…
Netwave
  • 40,134
  • 6
  • 50
  • 93
2
votes
2 answers

Is there a macro that automatically creates a dictionary from an enum?

An enum is clearly a kind of key/value pair structure. Consequently, it would be nice to automatically create a dictionary from one wherein the enum variants become the possible keys and their payload the associated values. Keys without a payload…
George
  • 2,451
  • 27
  • 37
2
votes
1 answer

What fragment specifiers (metavariable types) should be used in macro-by-example for a module path + type name?

I'm finding myself write code like this: // Of course I could add a use statement, but there's still undesireable duplication. // in the real code theres anywhere from 3 to 10 items in the tuple, // but that would just clog up this example pub fn…
Shelvacu
  • 4,245
  • 25
  • 44
2
votes
0 answers

Is there a way to extend the route attribute macros to automate Rocket configuration?

Below is the "Hello world" example of Rocket, which only has one route handler (hello()): #[macro_use] extern crate rocket; #[get("/hello//")] fn hello(name: &str, age: u8) -> String { format!("Hello, {} year old named {}!", age,…
at54321
  • 8,726
  • 26
  • 46
2
votes
1 answer

Expanding a recursive macro in rust, similar to serde_json but for HTML elements

#[macro_export] macro_rules! reactant { // Converts (function) {...} into element ( $f:ident $t:tt ) => { { let mut elem = HtmlElement::new($f); reactant!(@expand elem $t); elem } }; …
2
votes
1 answer

Is there a consistent compilation context inside a proc_macro_attribute function?

Take the example below: static COUNT: AtomicUsize = AtomicUsize::new(0); #[proc_macro_attribute] pub fn count_usages(_attr: TokenStream, item: TokenStream) -> TokenStream { let c = COUNT.fetch_add(1, Ordering::AcqRel); println!("Do stuff…
Eric
  • 33
  • 2
2
votes
1 answer

Writing const generic enum combinations using macro

Consider a toy struct with two const generic parameters: pub struct Foo([usize; N], [usize; M]); impl Foo { pub fn bar(&self) -> usize { N * M } } Let's say all…
MSR
  • 2,731
  • 1
  • 14
  • 24
2
votes
1 answer

Is it possible to implement this macro using only `macro_rules!`?

I'm trying to make a macro that would let me iterate through a list of types to reduce trait impl boilerplate. (I'm currently using a different macro based solution, but this seems more readable, if it's possible without adding a dependency.) This…
Dogbert
  • 212,659
  • 41
  • 396
  • 397
2
votes
0 answers

How to create a new identifier by concatenating string literals?

I'd like to be able to use a str input as ident inside a macro. The specific usage would be something like this: macro_rules! foo { ( $x:ident ) => { let $x = "stuff"; let $x_2 = "other_stuff" }; } So then the macro, when…
Dominus
  • 808
  • 11
  • 25
2
votes
1 answer

Difficulty with Recursive Macro

I'm trying to remove code duplication in this Rust code using a macro: enum AnyError { Error(Error), ParseIntError(ParseIntError), } impl From for AnyError { fn from(err: Error) -> AnyError { AnyError::Error(err) } } impl…
mlucy
  • 5,249
  • 1
  • 17
  • 21
2
votes
1 answer

Can a procedural macro be debugged as a function?

I'm trying to debug a complex procedural macro in a library I am using. Since I cannot use a debugger with macros and various macro expansion tools have proved useless here, I'm looking for an alternative. Can a procedural macro be run like a…
Jonathan Woollett-light
  • 2,813
  • 5
  • 30
  • 58