Attribute Macro macro_magic::export_tokens

source ·
#[export_tokens]
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.

Examples

Applied to a regular function definition:

#[export_tokens]
fn my_function() {
    println!("hey");
}

Applied to a module:

#[export_tokens]
mod my_module() {
    fn some_fn() {
        stuff();
    }
}

Applied to an impl requiring an override name:

#[export_tokens(impl_my_trait_for_my_item)]
impl MyTrait for MyItem {
    fn something() {
        do_stuff();
    }
}

Applied to a struct, but specifying an override name:

#[export_tokens(SomeOtherName)]
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.