Did you think Macros 1.1 was only for custom derives? Think again.
Note: Requires stable Rust 1.15.0 or nightly-2017-01-04 or later.
Two crates are required to define a macro.
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
```rust
/// Add one to an expression. procmacroexprdecl!(addone! => addoneimpl);
/// A function that always returns 2. procmacroitemdecl!(twofn! => twofnimpl); ```
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
```rust
procmacroexprimpl! { /// Add one to an expression. pub fn addone_impl(input: &str) -> String { format!("1 + {}", input) } }
procmacroitemimpl! { /// A function that always returns 2. pub fn twofn_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:
toml
[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:
toml
[lib]
proc-macro = true
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
```rust
two_fn!(two);
fn main() { let x = two(); let nine = addone!(x) + addone!(2 + 3); println!("nine = {}", nine); } ```
#[macro_use] extern crate
two different crates,
not just one.Licensed under either of
at your option.
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.