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

Can I append a variable name to the setter and getter function names in a macro?

I need to generate setters and getters for my FFI code. Is there a way I can append the variable name to the setter and getter function names in a macro? I tried stringify!($var + set) but it generated a bunch of errors. Is hiding it in a module for…
KDN
  • 485
  • 2
  • 7
  • 18
1
vote
1 answer

How do I add examples to macro documentation in rust?

When writing a macro, I would like to document it proprerly, and this includes examples. But when I try to do that in the same way as a regular function I get: [E0468]: an `extern crate` loading macros must be at the crate root I run cargo test on…
techhazard
  • 13
  • 2
1
vote
1 answer

The std documentation examples with question marks don't compile

I'm having extreme difficulties with the try! and ? macro to the point that I'm starting to question the very fabric of reality. I lifted the example below straight from the rust-docs and it still blows up in my face. Code: pub use…
user1870238
  • 447
  • 7
  • 20
1
vote
2 answers

Match an underscore instead of ident in macro

I'm creating a macro that matches two expressions and an identifier. I would like to be able to ignore the identifier if it's not needed, but the compiler seems to complain if I use _ there. My macro: macro_rules! if_some { ($x:expr, $id:ident,…
helios
  • 13,574
  • 2
  • 45
  • 55
0
votes
2 answers

Why is "macro expansion to text a lossy process"?

As disclaimed in the disclaimer section of cargo-expand's readme https://github.com/dtolnay/cargo-expand#disclaimer. Also, are there any guidelines that can be followed when writing/using macros that make the process less lossy/more likely that the…
ajp
  • 1,723
  • 14
  • 22
0
votes
1 answer

How to parse simple list of identifier in function-like proc macro?

I'd like to build a function-like proc_macro that accepts a list of type names, and then does some stuff with them. I don't need any fancy syntax, I just want something like my_macro!(foo::Bar, baz::Quuz, A, B); and I might want to, e.g. generate a…
ajp
  • 1,723
  • 14
  • 22
0
votes
0 answers

How do I call a function using the elements of an array as attributes

I'm new to rust and found myself tinkering with some macros. I decided, that I want to try making a macro, that can take an array and call a function, using the elements of that array as arguments for the function. This is what I have so…
Luh0
  • 105
  • 8
0
votes
1 answer

Reversing a list via a macro

I am trying reverse a list of "vec2's" at compile time using a macro: macro_rules! point_reverser { ([$([$x:expr, $y:expr]),*]) => { point_reverser!([$([$x, $y]),*] reversed: []) }; ([[$x:expr, $y:expr], $([$x_head:expr,…
jakebird451
  • 2,288
  • 4
  • 30
  • 45
0
votes
2 answers

Rust macro to generate custom arrays using byte slice

I want to make a macro in rust to generate a custom syntax array in order to use in a match: The match at it's baseline: match &self.text[self.offset..] { [b'f', b'o', b'r', b' ', ..] => { Some(CToken::new(CTokenKind::For,…
Matrix22
  • 3
  • 4
0
votes
1 answer

Rust conditional compilation (#[cfg(...)]) with names instead of hard-coded strings?

I'd like to use Rust's conditional compilation, e.g.: #[cfg(platform="value")] I'm wondering if there's a way to define a constant VALUE = "value", such that this can be written as #[cfg(platform=VALUE)] As I understand it, I'd need these defined…
0
votes
1 answer

How can I use Derived-macro attribute to store a generic struct in a Vec?

I'm working on implementing dynamic listeners for the communication layer of my embedded program. I want to store a Vec> in my Readers structure. Since T can change, I need to hide it behind a trait: Vec>…
totok
  • 1,436
  • 9
  • 28
0
votes
1 answer

Rust macro extremely slow (increase expenentially) and with high disk usage

I have a crate that generate code with macros using quote and proc_macro2::TokenStream. When the size of the generated code increase a little, the compilation time explodes exponentially, and the disk usage of my SSD stays at 100% for a few…
Tom
  • 4,666
  • 2
  • 29
  • 48
0
votes
1 answer

Rust: dynamic struct properties dependent on const

How do you define a struct with dynamic properties that depend on a const declaration in the body of the program? const DIMS: u32 = 12; struct Point { dim1: u32, dim2: u32, dim3: u32, dim4: u32, dim5: u32, ... dim{i}: u32 for…
Test
  • 962
  • 9
  • 26
0
votes
0 answers

Procedural macro in Rust to append arms to an existing enum

I am new to Rust + procedural macro's — apologies because I don't know what I am doing Background: I am using the rustdds crate to implement DDS. I have created extensive wrapper code to customize these domain participants to talk on custom topics…
0
votes
1 answer

Creating a Macro in Rust to take a Struct and turning into a Tuple of its field's types in order

Basically, the goal is to be able to take a struct like the following: struct Test { id : i64, name : String, data : HashMap, } And have the macro return a tuple of its field types in the order that we declared the…
Warren Niles
  • 184
  • 9