Macro macro_magic_macros::import_tokens
source · import_tokens!() { /* proc-macro */ }
Expand description
Allows you to import the tokens of an external item marked with
#[export_tokens]
whose path is already known at compile-time
without having to do any additional parsing.
If the path of the item is defined by the downstream programmer and is not “hard-coded”,
then you should instead use #[import_tokens_attr]
/
#[import_tokens_proc]
.
The macro lets you define as its argument a let variable declaration that will expand to that variable being set to the tokens of the specified external item at compile-time.
For example:
import_tokens!(let tokens = external_crate::SomeItem);
will expand such that a tokens
variable will be created containing the tokens for the
SomeItem
item that exists in an external crate. For this to work,
external_crate::SomeItem
must be the path of an item that has
#[export_tokens]
attached to it. The imported tokens wil be of
type TokenStream2
.
Unfortunately this macro isn’t very useful, because it is quite rare that you already know the path of the item you want to import inside your proc macro. Note that having the tokens for the path you want isn’t the same as having those tokens already expanded in the current context.
That said, this can be quite useful for scenarios where for whatever reason you have an item with a set-in-stone path whose tokens you need to access at compile time.