在Rust的程序宏中使用

在Rust的程序宏中使用

本文介绍了在Rust的程序宏中使用$ crate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 $crate 变量是,但是据我所知,它不能在过程宏中使用.还有另一种方法可以达到类似的效果吗?

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?

我有一个示例,大致要求我使用 quote 和每晚的Rust 编写类似的内容 >

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

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

我需要确保SomeTrait在范围内(#len引用的是代码段范围之外的整数).

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

我每天晚上都使用引号和syn来使用程序宏2.0,因为proc-macro-hack不适用于我. 这是示例我正在尝试概括.

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.

推荐答案

基于 https://github.com/rust-lang/rust/issues/38356#issuecomment-412920528 ,看来没有办法(截至2018-08年),也没有可以引用proc宏板条箱,也可以明确地引用其他任何板条箱.

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.

这篇关于在Rust的程序宏中使用$ crate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:31