Questions tagged [rust-decl-macros]

28 questions
1
vote
1 answer

Is there a way to pass a function as a parameter of a macro?

I'm trying to write a function similar to g_signal_connect_swapped in gtk+. macro_rules! connect_clicked_swap { ($widget: tt,$other_widget: expr,$function: ident) => { $widget.connect_clicked(|widget| $function($other_widget)) …
Kofi
  • 525
  • 6
  • 15
1
vote
2 answers

Use local bindings at call site in Rust "macro_rules" macros

Consider the following snippet: macro_rules! quick_hello { ($to_print:expr) => { { let h = "hello"; println!("{}", $to_print) } } } fn main() { quick_hello!(h); } If I compile it, I…
Dincio
  • 1,010
  • 1
  • 13
  • 25
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

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
0
votes
1 answer

How to concat two variables to create an identifier in a declarative macro?

I want to write a macro to support parameterized tests, and got the following code from AI, but got errors on one line: #[macro_export] macro_rules! parameterize { ($name:ident, $params:pat, {$($test_name:ident : ($($args:expr),*)),*},…
Zach
  • 5,715
  • 12
  • 47
  • 62
0
votes
2 answers

How can I operate on pairs of arguments to macro_rules?

I am building a simple feed forward neural network at compile time using const generics and macros. These are a bunch of matrices one after the other. I have created the network! macro, which works like this: network!(2, 4, 1) The first item is…
codearm
  • 185
  • 1
  • 9
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
0
votes
1 answer

Can I define a macro which will expand into a function call?

I've (naively) tried this, but it doesn't print anything to the screen: macro_rules! foo { ($suffix:tt, $arg:expr) => { concat!("foo", $suffix, "(", $arg, ")"); }; } fn foo_i32(x: i32) { println!("i32 {}", x); } fn foo_bool(x:…
Shmoopy
  • 5,334
  • 4
  • 36
  • 72
0
votes
2 answers

Macro to build enum with different "kinds" of elements

I am trying to come up with a macro which I would call like create_states!(S0, S1, final S2, final S3); It will create an enum to represent state machine states and some of them will be final (accepting) states - S2, S3. The resulting enum and its…
Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
0
votes
1 answer

Why do I get "unexpected token" when trying to call an inner macro with the argument of an outer macro?

I don't understand this failure while trying to pass the expression received by the higher! macro to the lower! macro: // A low-level macro using only Rust primitives. macro_rules! lower { (x, $a:expr) => { println!("x is {}", $a); …
iago-lito
  • 3,098
  • 3
  • 29
  • 54
0
votes
1 answer

Macro matching tokens recursive expansion

I'm triying to implement a macro that will expand a brainfuck program (after starting with some simpler code, in which I had problems coming up with a solution already: How to parse single tokens in rust macros). The problem is that at some point of…
Netwave
  • 40,134
  • 6
  • 50
  • 93
0
votes
2 answers

Tuple indexing in macro

I'm trying to index data tuple in macro that generate signature for trait implementation, but have some errors. Can I index tuple or need another solution? Hack with tuple_index I found in google but it not works for…
1
2