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

Why does the compiler not adds double brackets in a declarative macro automatically?

When creating an identifier in a declarative macro, it is mandatory to put additional brackets in the macro (double brackets). I was wondering why the compiler does not just (always) add the additional brackets. Example 1: this code will not compile…
J. Doe
  • 12,159
  • 9
  • 60
  • 114
1
vote
1 answer

Rust macro: capture exactly matching tokens

My goal is to write a macro expand! such that: struct A; struct B; struct Mut; expand!() => () expand!(A) => (A,) expand!(mut A) => (Mut,) expand!(A, mut B) => (A, Mut,) // etc [Edit] added trailing comma for consistent…
TheOperator
  • 5,936
  • 29
  • 42
1
vote
1 answer

How can I get "Bar" from "Foo::Bar" in rust macro?

Original demand: I want to implement a macro that converts Foo::* to Bar::*. Pseudo code will look like this: macro_rules! convert_foo_to_bar { ($v: ty, $p: path) => (<$v>::$p.name) } // convert_foo_to_bar!(Bar, Foo::A) -> Bar::A While $p.name…
Jason Sui
  • 13
  • 3
1
vote
0 answers

How to generate implementation of my trait for foreign type?

I have a trait HelloMacro pub trait HelloMacro { fn hello_macro(); } And I've already implemented proc macro crate for this trait. This is a usage, just like in the book: use hello_macro::HelloMacro; use…
NikBond
  • 743
  • 1
  • 5
  • 20
1
vote
4 answers

How do I create a match macro in rust?

I'm trying to complete Quiz #4 in the Rustlings exercises: // Write a macro that passes the quiz! No hints this time, you can do it! #[cfg(test)] mod tests { use super::*; #[test] fn test_my_macro_world() { …
chasahodge
  • 373
  • 1
  • 4
  • 11
1
vote
1 answer

invoke another macro in struct define

I made a simple macro that defines a struct. macro_rules! __body { ($($body:tt)+) => { $($body)+ }; } macro_rules! new_struct { ($(#[$attr:meta])* struct $name:ident { $($body:tt)+ } ) => { $(#[$attr])* struct $name { …
jxlczjp77
  • 11
  • 1
1
vote
1 answer

Treating idents as string in Rust macros

I have a configuration struct with some top level properties which I would like to put in sections. I order to provide deprecation warnings I made the following macro macro_rules! deprecated { ($config:expr, $old:ident, $section:ident, $new:ident)…
reish
  • 831
  • 7
  • 18
1
vote
1 answer

Macro that makes struct based on struct field's types

I am trying to make a macro that generates a struct with a new function implementation. The new function needs to call functions based on the type of the field and use the return value as the field value. The new implementation should end up working…
Gus
  • 151
  • 2
  • 15
1
vote
1 answer

Generate attribute value via macro

Suppose I have an ident input parameter named module_name. How can I generate the value of the attribute through this parameter? In simple terms, I want to generate something like this: macro_rules! import_mod { ( $module_name:ident ) => { …
Hentioe
  • 228
  • 1
  • 9
1
vote
1 answer

Macros with three optional parameters and one of them have to accept list of values

I have a structure with two optional fields and would like to provide special macro to simplify instances creation. The macro have to accept one required argument and three optional arguments. Last optional argument have to accept list of values.…
MaxV
  • 2,601
  • 3
  • 18
  • 25
1
vote
1 answer

How to implement a macro that defines a new public type and returns an instance of that type?

I want to implement a struct using macro_rules! because the generics require a lot of boilerplate and trait hunting. The struct in question has a hash table inside but the key and the value types are to be provided by the user. The code is as…
noconst
  • 639
  • 4
  • 15
1
vote
1 answer

Is there a way to "do macro stuff" in an attribute without a procedural macro?

Specifically, I'm trying to put macro output into a doc comment. I was excited that this does exactly what I want: /// foo /// #[doc="bar\n\nbaz"] /// /// quux struct Dummy; The next step would be to replace that string with my content. According…
Silly Freak
  • 4,061
  • 1
  • 36
  • 58
1
vote
1 answer

How do you get the type of an impl block in a Rust procedural macro?

I'm trying to write a Rust procedural macro that can be applied to an impl block like this; struct SomeStruct { } #[my_macro] impl SomeStruct { } I'm using syn and quote to parse and format TokenStreams in the macro. It looks something like…
Mendelt
  • 36,795
  • 6
  • 74
  • 97
1
vote
2 answers

Macro that wraps an arbitrary number of types

Is it possible to write a macro that defines an enum which wraps an arbitrary number of (distinct) input types? I'd like to do a kind of type-level match. type_switch!(i32 => println!("integer"), f32 => println!("float"), Foo =>…
pandaman
  • 215
  • 2
  • 6
1
vote
1 answer

How do I apply a macro attribute to a function defined in a separate module?

I'm interested in using wasm-bindgen via rust-webpack-template to compile Rust code to WebAssembly. However, I'd like to avoid directly wrapping my code with the #[wasm_bindgen] attribute macro directly so that I can separate out the function logic…
Stefan Novak
  • 763
  • 1
  • 6
  • 13