Attribute Macro macro_magic::export_tokens

source ·
Expand description

Can be applied to any item. Doing so will make the tokens for this item available for import by the other macros in this crate.

An optional argument can be provided specifying an override export name to use instead of the regular name of the item, such as #[export_tokens(MyCoolName)] or #[export_tokens(some_name)]. Syntactically this name is parsed as a syn::Ident and is then normalized by converting to snake_case. Note that because of this, MyCoolName would collide with my_cool_name, resulting in a compiler error if these items are being exported from the same module.

Note that some types of items, namely syn::ItemForeignMod, syn::ItemUse, syn::ItemImpl, and syn::Item::Verbatim, do not have an inherent concept of a naming ident, and so for these items specifying an override name is required or you will get a compiler error. This also applies to macro_rules! definitions that do not specify a name.

Note also that while you can presently attach #[export_tokens] to anything attributes can be attached to, some of these items do not exist at the module path level, and therefore cannot be accessed. You should only attach #[export_tokens] to items that are accessible by path from the location where you wish to use their tokens.


Applied to a regular function definition:

fn my_function() {

Applied to a module:

mod my_module() {
    fn some_fn() {

Applied to an impl requiring an override name:

impl MyTrait for MyItem {
    fn something() {

Applied to a struct, but specifying an override name:

struct MyStruct {
    field: u32,

Previously it was possible to access #[export_tokens] items defined in private/inaccessible contexts, however this was removed in 0.4.x.