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

Call `stringify!` inside macro

This macro compiles when invoked: macro_rules! remote_optional { ($remote:ident with=$def:ident $def_str:expr) => { impl $def { fn deserialize_option<'de, D>(deserializer: D) -> Result, D::Error> …
kpozin
  • 25,691
  • 19
  • 57
  • 76
3
votes
1 answer

Macro rule for matching a doc comment

Is there a way to match doccomments in Rust's macro_rules? I have a macro that generates an enum for a bunch of C constants that bindgen creates: macro_rules! id_enum { ( enum $name:ident: $typ:ty { $( $enum_name:ident => $value:expr ),* , } )…
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
3
votes
1 answer

Why can I not pass a captured token to a nested macro?

Multiple examples I've seen suggest that this should be possible, but it is apparently not: lib.rs: #![feature(trace_macros)] #[macro_export] macro_rules! inner_macro ( (f32) => {"float"}; ); #[macro_export] macro_rules! outer_macro { …
Cardano
  • 931
  • 1
  • 8
  • 14
3
votes
1 answer

Why does importing a custom derive Foo via `use some_crate::derive_foo` not work?

I want to use a custom derive macro that uses attributes. For Rust 2015, I wrote: #[macro_use] extern crate pest_derive; #[derive(Parser)] #[grammar = "grammar.pest"] pub struct MyParser; Using edition = '2018', extern crate is deprecated so…
flying sheep
  • 8,475
  • 5
  • 56
  • 73
3
votes
1 answer

print! macro gets executed out of order

A portion of my code looks like this: print_usage_instructions(); print!("Command: "); let stdin = io::stdin(); let mut line = String::new(); stdin.lock().read_line(&mut line).expect("Couldn't process the command."); println!("{}", line); The…
Sarp Başaraner
  • 505
  • 7
  • 17
3
votes
2 answers

How do I change the following from_str macro to from_json?

I have the following macro. Note that StringContent is an enum item. #[macro_export] macro_rules! from_str { ($json:expr) => { StringContent(String::from($json)) } } which allows me to write code like from_str!(r#"{ …
Harindaka
  • 4,658
  • 8
  • 43
  • 62
3
votes
2 answers

Specifying generic parameter to belong to a small set of types

Is it possible with to constrain a generic parameter to be one of the select few types without figuring out what traits precisely define those type? e.g. impl Data where T == u32 || T == u64 Sometimes it's tedious to figure out what all…
divbyzero
  • 313
  • 1
  • 11
2
votes
1 answer

how to generate const array by macro in Rust?

I want to generate this const array: const EXPS: [i64; 9] = [1, 10_i64.pow(1), 10_i64.pow(2), 10_i64.pow(3), 10_i64.pow(4), 10_i64.pow(5), 10_i64.pow(6), 10_i64.pow(7), 10_i64.pow(8)] by macro: define_exps!(9); where the argument 9…
Bingzheng Wu
  • 435
  • 3
  • 11
2
votes
0 answers

Can the code generated by a proc_macro be placed at the top level?

I am creating a proc_macro using proc_macro_attribute that creates a test module for the function it annotates. For example, if I have the following function: #[my_proc_macro] fn my_fn() -> i32 { 42 } The macro will create the equivalent to: fn…
Alexrs95
  • 392
  • 1
  • 3
  • 13
2
votes
3 answers

NEAR-sdk #[near_bindgen] colliding with other macros

I've put together a minimum working example, and you can see it here: https://github.com/synthesis-labs/near-mwe. In summary: I have a simple macro that checks that the owner (an arbitrary definition in the context of a struct of the contract) has…
Mike
  • 59
  • 5
2
votes
1 answer

Rustlang Macro $operator= seen as syntax error

So I wrote this macro in Rustlang to allow me to create multiple functions fast that accepts a bunch of values and then perform an arithmetic operation (or may be other operations, hence the need for it to remain dynamic enough) on them this way…
Henry Obiaraije
  • 301
  • 3
  • 10
2
votes
1 answer

How do I set a 'private' property for the default parameter in rust declarative macros

I want to write a declarative macros: log!("{} {} {}", private!(1), public!(2), 3); out : 2 parameter is printed as "" if parameter is not add "public" or add "private" macros, my code: macro_rules! log { ($($arg:tt)*) => { …
bu haha
  • 21
  • 1
2
votes
1 answer

Is there a way to automatically register trait implementors?

I'm trying to load JSON files that refer to structs implementing a trait. When the JSON files are loaded, the struct is grabbed from a hashmap. The problem is, I'll probably have to have a lot of structs put into that hashmap all over my code. I…
Big_Bad_E
  • 947
  • 1
  • 12
  • 23
2
votes
0 answers

Rust equivalent of #Define?

I have a trait that has functions which need to return constantly sized arrays. Since generic array currenly does not support generic expressions within a constant generic, I have had to change certain constants from trait consts to generic consts.…
2
votes
1 answer

Are all Rust attributes Macros?

Are all attributes in Rust implemented as Macros? Or some native attributes are created specially by the compiler/language and does not use the Macros mechanism? If there are attributes not created via Macros, how do I identify them?
Finlay Weber
  • 2,989
  • 3
  • 17
  • 37