Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $crate in Rust's procedural macros?

Tags:

I know what the $crate variable is, but as far as I can tell, it can't be used inside procedural macros. Is there another way to achieve a similar effect?

I have an example that roughly requires me to write something like this using quote and nightly Rust

quote!(      struct Foo {         bar: [SomeTrait;#len]      } ) 

I need to make sure SomeTrait is in scope (#len is referencing an integer outside the scope of the snippet).

I am using procedural macros 2.0 on nightly using quote and syn because proc-macro-hack didn't work for me. This is the example I'm trying to generalize.

like image 937
Daniel Fath Avatar asked Jul 06 '17 13:07

Daniel Fath


People also ask

What is Proc-macro in Rust?

Procedural macros allow you to run code at compile time that operates over Rust syntax, both consuming and producing Rust syntax. You can sort of think of procedural macros as functions from an AST to another AST. Procedural macros must be defined in a crate with the crate type of proc-macro .

Can you use macros in Rust?

The most widely used form of macros in Rust is the declarative macro. These are also sometimes referred to as “macros by example,” “ macro_rules! macros,” or just plain “macros.” At their core, declarative macros allow you to write something similar to a Rust match expression.


1 Answers

Based on replies from https://github.com/rust-lang/rust/issues/38356#issuecomment-412920528, it looks like there is no way to do this (as of 2018-08), neither to refer to the proc-macro crate nor to refer to any other crate unambiguously.

like image 199
Ekleog Avatar answered Sep 30 '22 19:09

Ekleog