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

Rust macro split expression into bytes and into byte vector

I am having issues getting this functionality to work. I basically need a macro that will accept a vector of bytes and an expression to be inserted into the vector. Example let mut bytes: Vec = vec![0x0; 6]; println!("{:?}", bytes); let a: u32…
Simon
  • 95
  • 1
  • 11
0
votes
1 answer

Generating a combination tuple vec using a macro

Suppose I have two sets. (a, b, c) and (p, q) I want to generate a vector like this using a macro. let v = vec![(a,p), (a, q), (b, p), (b, q), (c, p), (c, q)]; So this is the macro I was writing. There may be other ways to write this, but I want to…
Herne
  • 77
  • 6
0
votes
1 answer

Declarative marco isn't getting recursively invoked with repeated parameters

I want to write a macro, which generates a struct with a default fn new(...) function. If the desired struct has a String field, I want new to be a template function like fn new>(..., field: S,...). The problem occurred when creating…
rrr
  • 1
  • 2
0
votes
1 answer

How to use declarative macro nested invocation other function-like procedural macro, can it works?

I use a declarative macro nested invocation other procedural macro, from arr![1_u32;2] to arr_proc([1_u32;2]). I don't want to call arr_proc! directly. But I may got something wrong, when I compile the rust code. It seems like the compiler can not…
Colagy
  • 1
0
votes
0 answers

Write custom grammar for Rust macro in VSCode

I use crepe library which provides crepe! macro for Datalog-like syntax code. My theme is not working well with it. For example, in the following code: crepe! { ... Edge(x, y) <- Edge(y, x); } With my current theme VSCode renders <- not as…
Max Smirnov
  • 477
  • 3
  • 10
0
votes
1 answer

macro_rules presume type about arguments

tl;dr I think I want a macro declaration to infer type (how do I create this macro?) This macro fails to compile (rust playground) macro_rules! debug_assert_none { ($($arg:tt),+) => { $( if cfg!(debug_assertions) { …
JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63
0
votes
1 answer

How to use unicode in a rust macro argument?

This is rougly what I want to use: enum DashNumber { NegInfinity, Number(N), Infinity, } macro_rules! dn { (-∞) => { DashNumber::NegInfinity }; (∞) => { DashNumber::Infinity }; ($e:expr) => { …
Rainb
  • 1,965
  • 11
  • 32
0
votes
0 answers

Use procedural macros to make a modified copy of external code

Say I have some external library code (that is also generated from a procedural macro itself): struct Type1; struct Type2<'a> (&'a str); struct Type3<'a> (&'a str); // Expanded definition from a proc macro enum Enum<'a> { A, B, …
gust
  • 878
  • 9
  • 23
0
votes
1 answer

macro_rules repeatition does not yield the expected token type

I am trying to match user-provided lists in a macro, but the repeat-pattern does not match the same branches depending on whether I call the macro on single items directly, or whether I call it from a repeat block. Input code (playground…
lesurp
  • 343
  • 4
  • 19
0
votes
1 answer

how to read a "mut" keyword in a macro call on some identifiers?

I'm making a macro to iterate over any tuple of components in my ECS. In the main lines, here is how it goes : for (pos: &Position, vel: &Velocity) in iterate_over_components!(components; Position, Velocity) {[...]} Where Position and Velocity are…
0
votes
1 answer

Is there any way to "early return" in a declarative macro iterations?

I'm new to Rust's macros. I'd like to find a way to achieve a behaviour like this: early return in iterations I learned I can generate tuples using a declarative macro like this one: fn function(input) -> Result { // do…
cyvb
  • 3
  • 2
0
votes
1 answer

Indexing a tuple using a macro in Rust unexpected token

I am trying to create a macro to perform a certain action for every element in a tuple in order to compensate for not being able to iterate over them. I wrote some code that replicates my issue: fn main() { let tuple = (1, 2); macro_rules!…
0
votes
1 answer

Trying to implement trait to `std::io::Read` and `std::fs::DirEntry`

I am trying to implement my custom trait for both impl std::io::Read and std::fs::DirEntry - the idea is to implement trait both for directores as well as files, buffers and so on. use std::fs::DirEntry; use std::io::Read; trait MyTrait {} impl
ventaquil
  • 2,780
  • 3
  • 23
  • 48
0
votes
1 answer

Double vs single bracket in rust macros

I've been looking at rust macros recently and have found conflicting examples of macros using brackets (as laid out below). I'd like to know what the difference between each of these are and which one should be used when building macros. I'd also…
goastler
  • 223
  • 2
  • 6
0
votes
1 answer

How to expand a type alias (speifically a typle type) in Rust declarative macros?

I'm trying to adapt a macro that takes a variable number of type arguments, to a macro that takes a (possibly aliased) tuple type as its last argument: macro_rules! serialize_individually2 { ($ecs:expr, $ser:expr, $data:expr, ($( $type:ty),*)) =>…
bbarker
  • 11,636
  • 9
  • 38
  • 62