Platform Tools Release 30.0.3 (6597393)
Snap for 6533464 from f5ffad39744bfa7d9d8041737da0e4cff1a791d4 to sdk-release

Change-Id: If150c8345e92547f52623022d36b4114b2606722
tree: 3e74b89dc79b68576d6b68340ca040274a642fd7
  1. .github/
  2. src/
  3. tests/
  4. .cargo_vcs_info.json
  5. .gitignore
  6. Android.bp
  7. Cargo.toml
  8. Cargo.toml.orig
  9. LICENSE-APACHE
  10. LICENSE-MIT
  11. METADATA
  12. MODULE_LICENSE_APACHE2
  13. OWNERS
  14. README.md
README.md

Procedural macros in expression position

As of Rust 1.30, the language supports user-defined function-like procedural macros. However these can only be invoked in item position, not in statements or expressions.

This crate implements an alternative type of procedural macro that can be invoked in statement or expression position.

This approach works with any stable or nightly Rust version 1.31+.

Defining procedural macros

Two crates are required to define a procedural macro.

The implementation crate

This crate must contain nothing but procedural macros. Private helper functions and private modules are fine but nothing can be public.

» example of an implementation crate

Just like you would use a #[proc_macro] attribute to define a natively supported procedural macro, use proc-macro-hack's #[proc_macro_hack] attribute to define a procedural macro that works in expression position. The function signature is the same as for ordinary function-like procedural macros.

extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro_hack::proc_macro_hack;
use quote::quote;
use syn::{parse_macro_input, Expr};

#[proc_macro_hack]
pub fn add_one(input: TokenStream) -> TokenStream {
    let expr = parse_macro_input!(input as Expr);
    TokenStream::from(quote! {
        1 + (#expr)
    })
}

The declaration crate

This crate is allowed to contain other public things if you need, for example traits or functions or ordinary macros.

» example of a declaration crate

Within the declaration crate there needs to be a re-export of your procedural macro from the implementation crate. The re-export also carries a #[proc_macro_hack] attribute.

use proc_macro_hack::proc_macro_hack;

/// Add one to an expression.
///
/// (Documentation goes here on the re-export, not in the other crate.)
#[proc_macro_hack]
pub use demo_hack_impl::add_one;

Both crates depend on proc-macro-hack:

[dependencies]
proc-macro-hack = "0.5"

Additionally, your implementation crate (but not your declaration crate) is a proc macro crate:

[lib]
proc-macro = true

Using procedural macros

Users of your crate depend on your declaration crate (not your implementation crate), then use your procedural macros as usual.

» example of a downstream crate

use demo_hack::add_one;

fn main() {
    let two = 2;
    let nine = add_one!(two) + add_one!(2 + 3);
    println!("nine = {}", nine);
}

Limitations

  • Only proc macros in expression position are supported. Proc macros in pattern position (#20) are not supported.

  • By default, nested invocations are not supported i.e. the code emitted by a proc-macro-hack macro invocation cannot contain recursive calls to the same proc-macro-hack macro nor calls to any other proc-macro-hack macros. Use proc-macro-nested if you require support for nested invocations.

  • By default, hygiene is structured such that the expanded code can‘t refer to local variables other than those passed by name somewhere in the macro input. If your macro must refer to local variables that don’t get named in the macro input, use #[proc_macro_hack(fake_call_site)] on the re-export in your declaration crate. Most macros won't need this.

License