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

why rust macros have the same behavior with whatever delimiter?

It's just curiosity, I noted that macros have the same behavior independently the delimiter with we call the macro, for example println!(....) have same behavior of println!{....} or println![....]. Is this in fact so, or is it nuanced? and Exists…
al3x
  • 589
  • 1
  • 4
  • 16
0
votes
1 answer

Passing & and &mut through macro matched types

I've boiled down an issue I'm seeing into this snippet: macro_rules! test_impl { (&mut $_:ty) => { 1 }; (&$_:ty) => { 2 }; ($_:ty) => { 3 }; } macro_rules! test { ($val: literal, $($t:ty), *) => { ($val $(, test_impl!($t))*) } } fn…
mattlangford
  • 1,260
  • 1
  • 8
  • 12
0
votes
1 answer

How to parse a function with a declarative macro?

I need a declarative macro to parse a function. I wrote one: macro_rules! parse_fn { () => {}; ( $( #[ $Meta : meta ] )* $( pub )? fn $Name : ident $( $Rest : tt )* ) => { $crate::parse_fn! { as DEFINE_FN …
Kos
  • 1,547
  • 14
  • 23
0
votes
1 answer

repeat a group of tokens in a macro

I'm trying to create a macro-based finite state machine, and I'm having issues figuring out how to get my repeating pattern to capture multiple lines of tokens. I've tried moving the repetition block around and changing the delimiters between lines,…
Jeremy Meadows
  • 2,314
  • 1
  • 6
  • 22
0
votes
1 answer

Dynamic struct key in macro

Is there a way to make these two macros the same macro? macro_rules! bg_color_test { ($($color:path, $flag:literal),*$(,)?) => { let mut options = Options::default(); options.text = String::from("my text"); $( …
Dominik
  • 6,078
  • 8
  • 37
  • 61
0
votes
2 answers

Is it possible to have a macro for a hashmap literal with "key: value" syntax?

This maplit crate allows hashmap literals with => as a separator. I believe it's impossible to use macro_rules! to allow the : separator, but is it possible with a macro that processes a stream of tokens? Is it impossible to add { key: value } style…
Test
  • 962
  • 9
  • 26
0
votes
1 answer

Question on invoking another macro_rules in macro_rules definition

I'm implementing writing TLV packet to somewhat impl std::io::Write. First I implement WriteBE trait, whose write_be(&mut self, data: T) method can write data with type T to Self. (implementation details omitted) And I'm trying to use…
niiiiiil
  • 3
  • 2
0
votes
2 answers

Create sum of ones till N using macro in Rust

I would like to create a macro for transforming count!(5) into 1+1+1+1+1. The final reason is to use count!(N) - 1 into a structure definition where N is a const generics. macro_rules! count { (1) => {0}; (2) => { 1 + count!(1)}; ($n:…
allevo
  • 844
  • 1
  • 9
  • 20
0
votes
1 answer

Best strategy for custom errors with common fields in Rust

I have implemented a parser in Rust that can return different types of errors. Previously I had implemented it using different Structs without fields that implemented the std::error::Error trait. The issue is that I encountered two problems: The…
Genarito
  • 3,027
  • 5
  • 27
  • 53
0
votes
1 answer

Mimicking CraftingInterpeter's BINARY_OP macro

Came across this while trying to follow the excellent Crafting Interpreters book but using Rust instead of C. The book builds a stack based virtual machine, of which I have a simple Rust version that looks something like: struct VM { stack:…
Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
0
votes
1 answer

How should I DRY up repeated statistical calculations for every field in a Struct in Rust?

I have a large number of repeated data transformations that I'd like to DRY up as much as possible. e.g. struct A { first: Option, second: i64, ... } let data = vec![A{None, 1}, A{Some(2.), 3}, ...]; Currently, I have fairly repetive…
Montana Low
  • 81
  • 1
  • 1
  • 4
0
votes
1 answer

How can I merge a macro and a trait like in the serde crate?

When I use the Serialize macro of serde I use the Macro and Trait together so I don't need to import the trait additional. #[serde::Serialize)] struct Abc { a: 4 } How does it work? When I try to implement this, I can't find a working solution…
Corfus
  • 13
  • 4
0
votes
1 answer

Why doesn't this repetition pattern work in Rust macro?

I'm trying to write a macro that generalizes serde_yaml deserialization for any struct so I don't have to rewrite the same thing over and over again. The only thing that is messign me up right now is the repetition inside a…
0
votes
1 answer

How to use multiple macros inside macros in Rust?

I’d like to have a code that supposed to achieve this in end. counters!(NAME1, “unit1”, NAME2, “unit2”) // generates const NAME1 = 0; const NAME2 = 1; static mut counters = [AtomicUsize::new(0), AtomicUsize::new(0)]; static units = [“unit1”,…
user1685095
  • 5,787
  • 9
  • 51
  • 100
0
votes
1 answer

In a Rust macro, How to follow a symbol to get the token stream of another function's definition?

A macro-decorated function calls another, how to get the callee function's token stream in the macro? decorated function: #[follow] fn caller(){ return callee(); } callee: fn callee(){ ... // some…
Lin Lee
  • 213
  • 2
  • 8