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

Override the default implementation for a single field of a struct using `#[derive(Debug)]`

I have a struct containing general config data for a web application: #[derive(Debug)] pub struct AppConfig { pub db_host: String, pub timeout: Duration, pub jwt_signing_key: String, // more fields } jwt_signing_key is a symmetric jwt…
cameron1024
  • 9,083
  • 2
  • 16
  • 36
0
votes
1 answer

Code in procedural macro is not added to stream

I was trying a simple procedural macro following the example from the documentation, more specifically: #[proc_macro_attribute] pub fn show_streams(attr: TokenStream, item: TokenStream) -> TokenStream { println!("attr: \"{}\"",…
Dominus
  • 808
  • 11
  • 25
0
votes
1 answer

How to use macro's internal struct outside?

I wonder how to construct a struct whose definition is within a macro. The test code like this: Play Ground Test fn main() { #[macro_export] macro_rules! helper { ($a: expr) => {{ #[derive(Debug)] struct…
0
votes
1 answer

How to pass parameters loaded from configuration file to a procedural macro function?

Here is a problem I am trying to solve. I have multiple procedural macro functions that generate tables of pre-computed values. Currently my procedural macro functions take parameters in the form of literal integers. I would like to be able to pass…
Dilshod Tadjibaev
  • 1,035
  • 9
  • 18
0
votes
0 answers

Can I pass an identifier to a macro but have the macro "see" the data it represents?

Specs: Rust nightly build 1.53.0 unstringify! crate dependency https://docs.rs/unstringify/0.1.1/unstringify/macro.unstringify.html I am building a simple macro to call an attribute on a struct when the attribute's name is passed in as a string,…
Randall Coding
  • 463
  • 5
  • 15
0
votes
0 answers

How do dynamically set attributes on match arms using macro_rules

I have an existing macro definition that captures the attributes. Inside that definition, I'm trying to filter and set the attributes selectively. I have a peculiar problem where I have to set attributes for match arms in one of these…
0
votes
0 answers

How to build macro which will import modules from directory?

Let's say I have following folder structure: src ├── lib.rs └── models ├── gate.rs ├── load_balancer.rs ├── mod.rs ├── processor.rs └── storage.rs Inside models/mod.rs I want to import everything that sits in same directory, however,…
Marcin
  • 179
  • 2
  • 14
0
votes
2 answers

Check Value of bool in Rust Macro

I'd like to create a macro that checks the value of a provided bool and returns a string based on that value. I tried this: macro_rules! dbg_bool{ () => {}; ($val:expr $(,)?) => { match $val { //if $val is true return a…
ANimator120
  • 2,556
  • 1
  • 20
  • 52
0
votes
1 answer

Get trait name and generic parameters for ItemImpl with syn

I have a code like this: #[my_attribute] impl Foo for Bar where T: Baz { ... } How to get the Foo part from ItemImpl?
Vlad
  • 3,001
  • 1
  • 22
  • 52
0
votes
1 answer

How do I write a macro that returns the implemented method of a struct based on a string?

Inspired by How to get struct field names in Rust?, I want to get an implemented method of the struct based on a str, something like: macro_rules! comp { (struct $name:ident { $($field_name:ident : $field_type:ty,)* } impl…
bruceyuan
  • 421
  • 1
  • 3
  • 8
0
votes
0 answers

Lookup for all invocations of the particular `proc-macro` inside another crate

I have a build script where I have access to a lot of information about another crate via cargo_metadata. Now I'm looking for the way to find all invocations of a specific proc-macro inside the another crate. Theoretically, I can parse another…
MaxV
  • 2,601
  • 3
  • 18
  • 25
0
votes
1 answer

How can a procedural macro check a generic type for Option> and flatten it down to a single Option?

I'm writing a derive procedural macro where all of the values are converted to Options. The problem is that any Option fields in the struct can be contained within these Option types. On its own, this isn't much of an issue until I start to…
Abeltensor
  • 132
  • 1
  • 10
0
votes
1 answer

Can I call a macro right inside the struct body?

I'm relatively new to Rust and I'm exercising with macros. The goal is to archive some sort if React-ish syntax within Rust lang. I’m aware of the drastic DSL approach like jsx!(
), which is a good solution but of another topic. I’m seeking…
hackape
  • 18,643
  • 2
  • 29
  • 57
0
votes
1 answer

How to pass a visibility token to the bitflags! macro?

I'm using the bitflags crate. I want to automatically generate some other implementations for my types, so I tried to call the bitflags! macro from another macro. This worked fine until I wanted to be able to handle a visibility token. I tried the…
Ross MacArthur
  • 4,735
  • 1
  • 24
  • 36
0
votes
0 answers

How do I use Rust macros in the same crate?

I have the sources as: src/ main.rs memory.rs chunk.rs memory.rs I have a macro: #[macro_export] macro_rules! grow_capacity { ( $x:expr ) => { { if $x < 8 { 8 } else { $x * 2 } } }; } chunk.rs I want to…
knh190
  • 2,744
  • 1
  • 16
  • 30