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

How can I concatenate a string to an ident in a macro derive?

I need to create a macro derive where the name is part of the function name. (This code does not work, it is only to show the problem) fn impl_logic(ast: &syn::DeriveInput) -> TokenStream { let name:&syn::Ident = &ast.ident; let gen =…
6
votes
2 answers

Can we get the source code location of the caller in a procedural macro attribute?

I have requirement to get the source location of the caller of every method. I am trying to create a proc_macro_attribute to capture the location and print it. #[proc_macro_attribute] pub fn get_location(attr: TokenStream, item: TokenStream) ->…
Ayush Mishra
  • 567
  • 1
  • 7
  • 19
6
votes
1 answer

Proper way to handle a compile-time relevant text file passed to a procedural macro

I have a requirement to pass to a procedural macro either a text file or the contents of a text file, such that the procedural macro acts based on the contents of that text file at compile time. That is, the text file configures the output of the…
Henry Gomersall
  • 8,434
  • 3
  • 31
  • 54
6
votes
0 answers

How to make a macro expand before stringifying it in Rust?

I'm trying to write a quine in Rust using only macros. In order to do that, I'm embedding the main function into macro f1, and trying to embed a literal representation of f1 in f2 with stringify!. Here is my code so far: macro_rules!f1{()=>(fn…
Jon Nimrod
  • 335
  • 1
  • 2
  • 12
6
votes
0 answers

Triggering macro expansion in build.rs

I want to create a macro using the crate cpp, however this crate has issues with macro expansion. Context The crate works by taking cpp!{ ... } macros of inline C code, compiling their contents as functions, and replacing the macro call with the…
Locke
  • 7,626
  • 2
  • 21
  • 41
6
votes
1 answer

Calculating maximum value of a set of constant expressions at compile time

I'm trying to calculate the maximum value of a set of constants at compile time inside a Rust procedural macro (a derive macro). The macro looks something like: fn get_max_len() -> TokenStream { // Each TokenStream represents a constant…
Richard Matheson
  • 1,125
  • 10
  • 24
6
votes
2 answers

Is it possible to create a macro that implements Ord by delegating to a struct member?

I have a struct: struct Student { first_name: String, last_name: String, } I want to create a Vec that can be sorted by last_name. I need to implement Ord, PartialOrd and PartialEq: use std::cmp::Ordering; impl Ord for Student { …
Haffix
  • 128
  • 2
  • 6
6
votes
1 answer

Pass entire macro input to another macro

I'm trying to make a simple macro that invokes vec! with whatever it receives then does some simple processing before returning the new vector: macro_rules! sorted_vec { ($x:expr) => { { let v = vec![$x]; …
anderspitman
  • 9,230
  • 10
  • 40
  • 61
5
votes
2 answers

Why is a warning suppressed when the code is inside of a macro?

The following code produces one warning but I expected two warnings; one for each time I wrote CString::new("A CString").unwrap().as_ptr(): use std::ffi::CString; use std::os::raw::c_char; extern "C" { fn puts(s: *const c_char); } macro_rules!…
Cesc
  • 904
  • 1
  • 9
  • 17
5
votes
2 answers

Interpolate `ident` in string literal in `macro_rules!`

Is it possible to interpolate a macro_rules! variable, of type ident, into a string literal in a macro? In other words is it possible to "escape" the double quotes of the literal? // `trace_macros!` requires…
BallpointBen
  • 9,406
  • 1
  • 32
  • 62
5
votes
1 answer

How to write a macro that displays the file and line number and a variable number of arguments?

I've found several useful macros in Rust, namely: file!(), line!(), stringify!() I've also found that Rust allows macros with variable arguments, as stated here: macro_rules! print_all { ($($args:expr),*) => {{ $( …
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
5
votes
1 answer

Can I rewrite this macro_rules! macro in a way that works with rustfmt?

I would like to use a macro to generate identical impl blocks for multiple concrete types. My code currently looks something like this: macro_rules! impl_methods { ($ty:ty, { $($method:item);+} ) => { impl $ty { $($method)+ } }; …
cmyr
  • 2,235
  • 21
  • 30
5
votes
0 answers

What is a streamlined approach to processing helper attributes in derive macros when using Rust?

What is a streamlined approach to processing helper attributes in derive macros when using Rust? To illustrate what I'm looking for, I've defined a derive macro called Duplicate that creates a new struct that contains all of the elements in the old…
wyer33
  • 6,060
  • 4
  • 23
  • 53
5
votes
1 answer

How to expand subpatterns in recursive macro_rules?

I am writing a macro to conveniently match nested structure in an enum typed variable to a compile-time template. The idea is to leverage Rust's pattern matching to enforce specific values in certain locations of the structure, or bind variables to…
MB-F
  • 22,770
  • 4
  • 61
  • 116
5
votes
2 answers

Rust macros: Calling function dependent on expression

I have a three different functions, of which I want to call one based on a macro argument. This argument should be pre-processed, which is why I thought I need to write it as expr. However, I can't seem to find a way to distinguish different cases…
MLStudent
  • 51
  • 1
  • 2