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

Escape $ dollar sign in macro_rules

How can I escape a dollar sign for a macro like this? macro_rules! test { ($ $name:ident) => { println!(stringify!($name)); }; } fn main() { test!($abc); } I want to get abc. I've tried using $$ and a bunch of possible ways to…
Jack
  • 1,893
  • 1
  • 11
  • 28
3
votes
2 answers

proc macro not found

My main function is decorated with two proc macros like this: #[paw::main] #[tokio::main] pub async fn main(args: Args) -> Result<()> This compiles and runs as I expect it but VS code's rust-analyzer gives me this error on both lines of attribute…
Thorkil Værge
  • 2,727
  • 5
  • 32
  • 48
3
votes
1 answer

Exporting declarative macro that uses functions defined in crate

I'm trying to export a macro that uses from some functions defined in the crate. Something like this for e.g in a crate named a_macro_a_day // lib.rs pub fn a() {} #[macro_export] macro_rules! impl_helper_funcs { use crate::a; // error…
twitu
  • 553
  • 6
  • 12
3
votes
1 answer

How to export function and macro with the same name?

Is it possible to export a function and a macro with the same name from a module? Example lib.rs mod log; fn foo() { log::info!(""); log::info(""); } In log.rs: Using pub(crate) use info; conflicts with pub fn info() { .. } Using…
Pàpas
  • 43
  • 3
3
votes
1 answer

Can't make this macro rule compile: type annotation needed

In the following code, I really don't find a way to make the macro rule args! compile, without changing its syntax like used in the main function: use std::rc::Rc; trait Checker: Fn(i32) -> bool {} impl Checker for F where F: Fn(i32) -> bool…
yolenoyer
  • 8,797
  • 2
  • 27
  • 61
3
votes
2 answers

How to write a procedural macro that implements a generic trait, fx. Add?

I have a Module trait and I would like to write a procedural macro that implements Add, Sub, and Mul for any implementation of the Module trait. Given a struct Foo that implements Module, the resulting code should look like this impl
AlbertGarde
  • 369
  • 1
  • 13
3
votes
1 answer

Procedural attribute macro to inject code at the beginning of a function

I'm trying to build a simple attribute which will inject a let binding at the beginning of a function, so the result would be: #[foo] fn bar(){ // start injected code let x = 0; // end injected code ... } I've gotten to this: use…
Dominus
  • 808
  • 11
  • 25
3
votes
2 answers

Why does Rust think my private type must be public unless I use pub(crate)?

I'm using a macro to generate a module, and that module defines a function that returns a type that the user passes in: macro_rules! generate_mod { ($name:ident: $type:ty = $e:expr) => { mod $name { use super::*; …
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
3
votes
2 answers

Passing nested struct field path as macro parameter

I am fairly new to Rust, and I am having trouble getting the following code to compile: #![feature(trace_macros)] fn main() { #[derive(Debug)] struct Inner { value: u8 } #[derive(Debug)] struct Outer { inner:…
Big Monday
  • 590
  • 1
  • 5
  • 15
3
votes
1 answer

Is there a streamlined way to invoke java functions from rust?

My understanding of invoking java from rust via the jni crate involves a bit of boilerplate that looks like je.call_method(self.rimuru, "shell2Pixels", "(II[B)V", &[ JValue::from(width), JValue::from(height), …
Mutant Bob
  • 3,121
  • 2
  • 27
  • 52
3
votes
1 answer

Can rust macros generate getters that are Copy aware?

derive-getters is a nice crate that will create getters for you. However, it always generates getters that return a reference, even for Copy types. For example for this struct: #[derive(Getters)] pub struct MyCheesyStruct { x: i64, y:…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
3
votes
0 answers

Call attribute macro in the implementation of another attribute macro

I cannot find how to inject a attribute macro in the result of my attribute macro. Here is an example of a kind of injection: use quote::quote; use proc_macro::TokenStream; #[proc_macro_attribute] pub fn my_test(_: TokenStream, item: TokenStream)…
Corebreaker
  • 357
  • 1
  • 11
3
votes
1 answer

Conditionally generate a From impl based on field type

I'm trying to create a generic impl for generating a From/Into based on different field types. Link to Playground I'm seeing the following issues: error[E0425]: cannot find value `item` in this scope --> src/lib.rs:23:21 | 23 | …
Jason
  • 33
  • 5
3
votes
1 answer

Rust: cannot find macro

I'm trying to run rust code from the postgres_types documentation. The example code can be found here: postgres_types my rust environment: cargo --version cargo 1.40.0-nightly (5da4b4d47 2019-10-28) rustc --version rustc 1.40.0-nightly…
James Schinner
  • 1,549
  • 18
  • 28
3
votes
1 answer

How do I provide optional generics as macro_rules arguments?

I want to use macro_rules to create an implementation for a trait. The types should be given as macro arguments. However, some of those types may contain lifetimes, so I need them. I also have a generic type from inside the macro. The result should…
llogiq
  • 13,815
  • 8
  • 40
  • 72