proc-macro-hack 0.2.0

Procedural functionlike!() macros using only Macros 1.1
Documentation

Procedural functionlike!() macros using only Macros 1.1

Build Status Latest Version

Did you think Macros 1.1 was only for custom derives? Think again.

Note: Currently only works with nightly-2016-11-06 because of rust-lang/rust#38706. The bug has been fixed in master and the fix will be in the next nightly after nightly-2017-01-01 as well as backported to 1.15.0-beta.

Defining procedural macros

Two crates are required to define a macro.

The declaration crate

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

https://github.com/dtolnay/proc-macro-hack/tree/master/demo-hack

#![feature(proc_macro)]

#[macro_use] extern crate proc_macro_hack;
#[macro_use] extern crate proc_macro_hack_impl;

/// Add one to an expression.
proc_macro_expr_decl!(add_one! => add_one_impl);

/// A function that always returns 2.
proc_macro_item_decl!(two_fn! => two_fn_impl);

The implementation crate

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

https://github.com/dtolnay/proc-macro-hack/tree/master/demo-hack-impl

#![feature(proc_macro, proc_macro_lib)]

#[macro_use] extern crate proc_macro_hack;
#[macro_use] extern crate proc_macro_hack_impl;

proc_macro_expr_impl! {
    /// Add one to an expression.
    pub fn add_one_impl(input: &str) -> String {
        format!("1 + {}", input)
    }
}

proc_macro_item_impl! {
    /// A function that always returns 2.
    pub fn two_fn_impl(input: &str) -> String {
        format!("fn {}() -> u8 {{ 2 }}", input)
    }
}

Both crates depend on proc-macro-hack which itself has a declaration crate and implementation crate:

[dependencies]
proc-macro-hack = "0.2"
proc-macro-hack-impl = "0.2"

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

[lib]
proc-macro = true

Using procedural macros

Users of your crate depend on your declaration crate and implementation crate, then use your procedural macros as though it were magic. They even get reasonable error messages if your procedural macro panics.

https://github.com/dtolnay/proc-macro-hack/tree/master/example

#![feature(proc_macro)]

#[macro_use] extern crate demo_hack;
#[macro_use] extern crate demo_hack_impl;

two_fn!(two);

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

Limitations

  • The input to your macro cannot contain dollar signs.
  • Users of your macro need to #[macro_use] extern crate two different crates, not just one.
  • Your macro must expand to either an expression or zero-or-more items, cannot sometimes be one or the other depending on input.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.