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
1
vote
1 answer

Is it possible to change/generate code through derive macro inside of the struct that uses that macro?

I was curious whether it is possible to change the code inside the struct that uses derive macro, or are you only limited to generating the new code outside? Example Adding another field to the Building struct through…
1
vote
1 answer

How can I remove files with a specific file extension in Rust

I need to remove all the files with ".html" extension from a specific directory in Rust. Do you have any ideas of how can I do that?
1
vote
1 answer

How to get both name and value of variable in a macro?

Is it possible to make a Rust macro that can access both the name and value of a variable passed as a parameter? let my_variable: i32 = 5; printvar!(my_variable); // => to print "my_variable = 5" For example with C macros we can use the #…
JShorthouse
  • 1,450
  • 13
  • 28
1
vote
1 answer

Refer to generic type of struct in macro

I need to use an attribute of the generic type of a struct in a macro. A slightly contrived but minimal example would be if I wanted to implement a method for a generic struct, that returned the minimum value of its generic type. struct Barn { …
road_rash
  • 121
  • 1
  • 7
1
vote
0 answers

Is there a way to use internal symbols in a Rust macro?

Suppose my library definespub struct MyStruct { a: i32 }. Then, suppose I define a procedural macro my_macro!. Perhaps I want my_macro!(11) to expand to MyStruct { a: 11 }. This proves that I must have used my_macro! to obtain MyStruct, since the a…
M. Taylor
  • 71
  • 1
  • 5
1
vote
2 answers

How do I early return in declarative macros and infer the return type?

I have a macro, which just generates an instance of a struct, as below: macro_rules! foo { () => {{ let baz_val = baz(); let bar_val = match bar() { Ok(val) => val, Err(err) => { …
Eray Erdin
  • 2,633
  • 1
  • 32
  • 66
1
vote
1 answer

Check if macro param is defined in optional capture (Declarative Macro Syntax)

I am creating a fairly complex macro and can't use the overloading syntax to create my macros. I am trying to conditionally check if a prop has been defined within an optional capture group or not: macro_rules! foo { (name: $name: ident$(,…
sno2
  • 3,274
  • 11
  • 37
1
vote
1 answer

How to match dot in macro and reconstruct original set of tokens?

I'm trying to write a macro that will expand this: let res = log_request_response!(client.my_call("friend".to_string(), 5)); Into this: let res = { debug!("Request: {}", args_separated_by_commas); let res =…
Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44
1
vote
2 answers

Is there a macro I can use to expect a variant of an enum and extract its data?

Given an enum like struct Earth { water: usize } struct Mars { redness: usize } enum World { Mars(Mars), Earth(Earth), } A common pattern I write is fn something_expecting_mars(planet: World) { let mars = match planet { …
user82395214
  • 829
  • 14
  • 37
1
vote
0 answers

How to match an argument with dots in Rust macros?

I am writing a program and it contains a lot of matchblocks as I keep calling methods and functions that return Result struct type results. So I was thinking maybe a macro will reduce the amount of code. And the final macro is like…
LunarEclipse
  • 661
  • 1
  • 4
  • 14
1
vote
0 answers

How does rust make a decision on what macro branch to use?

Consider the following code-snippet #[macro_export] macro_rules! test { ($a:literal) => { $a }; ($a:expr) => { $a }; } fn func() { let a = test!(0); let b = test!(a); let c = test!(-a); } Why does the third assignment not work?…
1
vote
1 answer

Why does the following macro expect a semi-colon when called?

I'm trying to write a macro to switch between rayon's par_iter and std's iter depending on a build feature (possibly getting ahead of myself, as I've not read a lot on macros yet). A macro seems a bit better to deal with than a function here, since…
bbarker
  • 11,636
  • 9
  • 38
  • 62
1
vote
2 answers

How do I get an identifer from an expression argument in a macro?

I have a constant value defined by a variable: const VAL: usize = 32; I want to make a function like this: macro_rules! valfn { ($val:expr) => { pub fn $val () -> () { // here val needs to be a ident some_other_fn($val) …
user318904
  • 2,968
  • 4
  • 28
  • 37
1
vote
1 answer

Unit testing Rust Syn crate

From the Syn documentation: Syn operates on the token representation provided by the proc-macro2 crate from crates.io rather than using the compiler's built in proc-macro crate directly. This enables code using Syn to execute outside of the context…
1
vote
1 answer

How to write a macro that splits a byte into a tuple of bits of user-specified count?

I would like to have macro splitting one byte into tuple with 2-8 u8 parts using bitreader crate. I managed to achieve that by following code: use bitreader::BitReader; trait Tupleprepend { type ResultType; fn prepend(self, t: T) ->…
sbagin13
  • 105
  • 4