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

How to store a list of types using a Rust macro?

I am using some macros that look like this: macro_rules! deserialize_individually { ($ecs:expr, $de_ser:expr, $data:expr, $( $type:ty),*) => { $( // *** omitted *** )* }; } They take a long list of $type and are used in a…
bbarker
  • 11,636
  • 9
  • 38
  • 62
0
votes
0 answers

Why can't macros see variables in scope?

I apparently don't understand how scoping works with regards to macros. I'm trying to write a macro using the TT muncher pattern. I think I need to have an initial matching arm which declares a variable, then munching arms which build it as they…
Edward Peters
  • 3,623
  • 2
  • 16
  • 39
0
votes
1 answer

Can I specify an operator or other syntactic literal within a macro input?

First, I know this is not a good use of macros but I'm learning what I can do. I have a struct Rational: pub struct Rational{ pub n: i128, pub d : i128 } I have a macro that builds these: macro_rules! rat{ ($n : expr, $d : expr) => { …
Edward Peters
  • 3,623
  • 2
  • 16
  • 39
0
votes
0 answers

How can I write a Rust macro and what type should I use?

I have an implementation for a struct that needs to be implemented on multiple structs with an argument for every struct. My impl statement is as follows: #[rocket::async_trait] impl<'r> FromRequest<'r> for ReadMe { type Error = AuthErrors; …
piano1029
  • 21
  • 4
0
votes
1 answer

Rust macro with dots in token trees

I'm trying to create a Rust macro that generates endpoints like "/api/v2/stats/". I thought it would be cool to make it like warp path does it: warp::path!("sum" / u32 / u32). But in warp, they do not need to support token trees with dots,…
rsalmei
  • 3,085
  • 1
  • 14
  • 15
0
votes
1 answer

Some explanation needed regarding custom multiplications in Rust

1.) I have made a big knot in my code it seems. I defined my own structs e.g. struct State { // some float values } and require them be multiplied by f64 + Complex64 and added together member-wise. Now I tried to abstract away the f64 and Complex64…
exocortex
  • 375
  • 2
  • 9
0
votes
0 answers

Rust Procedural Macro - macro expansion ignores token `,`

I have the following trivial procedural macro in one crate (it was more than this, but I distilled it to be as simple as possible while still causing the issue to occur) #[proc_macro] pub fn the_macro(input: TokenStream) -> TokenStream { …
me163
  • 1
0
votes
1 answer

Macros, How to generate enum variant from str literal?

I have an enum with over 100 variants. and I have to get each of its variants from a string. Something like this. enum VeryLongEnum { A, B, C, D, E, } impl From<&'static str > for VeryLongEnum { fn from(s: &'static str) ->…
al3x
  • 589
  • 1
  • 4
  • 16
0
votes
0 answers

Expanding function type in rust macro

I'm trying to write a small program that hooks IAT entries. Inside the hook, for now I want to just print the arguments and invoke orginal function (whose address I stored beforehand in a static context). As I want to hook multiple entries, I was…
Rob D
  • 71
  • 1
  • 6
0
votes
1 answer

How to import required types inside of a custom derive macro?

I have a custom derive macro which generates some code for a struct it is applied to. That code uses few types (bytes::Buf, ect) that need to be imported. #[derive(MyMacro)] struct S { ... } What is the correct way to handle it? If I leave it with…
C.M.
  • 3,071
  • 1
  • 14
  • 33
0
votes
1 answer

Expose struct generated from quote macro without appearing out of nowhere

How can I expose a struct generated from the quote macro in my derive macro without having to introduce a struct name out of the blue in my usage file (due to macro expansion)? To illustrate the point, currently, my code looks something like…
code
  • 5,690
  • 4
  • 17
  • 39
0
votes
1 answer

How to implement a Rust macro using module paths as a macro function parameter?

As asked by @isaactfa, PS C:\...> rustc --version rustc 1.62.1 (e092d0b6b 2022-07-16) // Cargo.toml [dependencies] diesel = { version = "1.4.5", features = ["postgres", "sqlite", "chrono"] } chrono = "0.4" libsqlite3-sys = { version = "^0",…
MikeTheSapien
  • 161
  • 3
  • 8
0
votes
1 answer

expected one of `!` or `::`, found _ - rust macro with items

I am trying to implement a rust macro with items. Both Osc and Multiply implement a trait with const id: [char; 3] and fn new(cctx: &CreationalContext) -> Self The code is: macro_rules! ifid { (($id:expr, $ctx:expr) $($node:item),+) => { …
Michael Barr
  • 35
  • 1
  • 6
0
votes
1 answer

Procedural macro inside Syntactic macro error

lets say I wan't to generate this with just syntactic macros: struct Foo { bar: [Bar; 100], gee: [Gee; 100], } with just this macro call: make_struct!(100; Bar, Gee); The first and obvious problem is is that lowercase bar/gee which as far…
cdecompilador
  • 178
  • 2
  • 9
0
votes
1 answer

Rust macro to format arguments over multiple formats

TL;DR I'm trying to write a macro that will do the following transformation: magic_formatter!(["_{}", "{}_", "_{}_"], "foo") == [format!("_{}", "foo"), format!("{}_", "foo"), format!("_{}_", "foo")] (a solution that will give…
NivPgir
  • 63
  • 4