• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H03-May-2022-2320

src/H03-May-2022-953724

tests/H03-May-2022-4835

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D24-May-202074 65

.gitignoreH A D25-Apr-201918 32

Cargo.tomlH A D24-May-20201.2 KiB4537

Cargo.toml.orig-cargoH A D24-May-2020716 2823

LICENSE-APACHEH A D25-Apr-201910.6 KiB202169

LICENSE-MITH A D25-Apr-20191 KiB2622

README.mdH A D24-May-20205.9 KiB145105

README.md

1Procedural macros in expression position
2========================================
3
4[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/proc--macro--hack-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/proc-macro-hack)
5[<img alt="crates.io" src="https://img.shields.io/crates/v/proc-macro-hack.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/proc-macro-hack)
6[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-proc--macro--hack-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/proc-macro-hack)
7[<img alt="build status" src="https://img.shields.io/github/workflow/status/dtolnay/proc-macro-hack/CI/master?style=for-the-badge" height="20">](https://github.com/dtolnay/proc-macro-hack/actions?query=branch%3Amaster)
8
9As of Rust 1.30, the language supports user-defined function-like procedural
10macros. However these can only be invoked in item position, not in
11statements or expressions.
12
13This crate implements an alternative type of procedural macro that can be
14invoked in statement or expression position.
15
16This approach works with any stable or nightly Rust version 1.31+.
17
18## Defining procedural macros
19
20Two crates are required to define a procedural macro.
21
22### The implementation crate
23
24This crate must contain nothing but procedural macros. Private helper
25functions and private modules are fine but nothing can be public.
26
27[&raquo; example of an implementation crate][demo-hack-impl]
28
29Just like you would use a #\[proc_macro\] attribute to define a natively
30supported procedural macro, use proc-macro-hack's #\[proc_macro_hack\]
31attribute to define a procedural macro that works in expression position.
32The function signature is the same as for ordinary function-like procedural
33macros.
34
35```rust
36extern crate proc_macro;
37
38use proc_macro::TokenStream;
39use proc_macro_hack::proc_macro_hack;
40use quote::quote;
41use syn::{parse_macro_input, Expr};
42
43#[proc_macro_hack]
44pub fn add_one(input: TokenStream) -> TokenStream {
45    let expr = parse_macro_input!(input as Expr);
46    TokenStream::from(quote! {
47        1 + (#expr)
48    })
49}
50```
51
52### The declaration crate
53
54This crate is allowed to contain other public things if you need, for
55example traits or functions or ordinary macros.
56
57[&raquo; example of a declaration crate][demo-hack]
58
59Within the declaration crate there needs to be a re-export of your
60procedural macro from the implementation crate. The re-export also carries a
61\#\[proc_macro_hack\] attribute.
62
63```rust
64use proc_macro_hack::proc_macro_hack;
65
66/// Add one to an expression.
67///
68/// (Documentation goes here on the re-export, not in the other crate.)
69#[proc_macro_hack]
70pub use demo_hack_impl::add_one;
71```
72
73Both crates depend on `proc-macro-hack`:
74
75```toml
76[dependencies]
77proc-macro-hack = "0.5"
78```
79
80Additionally, your implementation crate (but not your declaration crate) is
81a proc macro crate:
82
83```toml
84[lib]
85proc-macro = true
86```
87
88## Using procedural macros
89
90Users of your crate depend on your declaration crate (not your
91implementation crate), then use your procedural macros as usual.
92
93[&raquo; example of a downstream crate][example]
94
95```rust
96use demo_hack::add_one;
97
98fn main() {
99    let two = 2;
100    let nine = add_one!(two) + add_one!(2 + 3);
101    println!("nine = {}", nine);
102}
103```
104
105[demo-hack-impl]: https://github.com/dtolnay/proc-macro-hack/tree/master/demo-hack-impl
106[demo-hack]: https://github.com/dtolnay/proc-macro-hack/tree/master/demo-hack
107[example]: https://github.com/dtolnay/proc-macro-hack/tree/master/example
108
109## Limitations
110
111- Only proc macros in expression position are supported. Proc macros in pattern
112  position ([#20]) are not supported.
113
114- By default, nested invocations are not supported i.e. the code emitted by a
115  proc-macro-hack macro invocation cannot contain recursive calls to the same
116  proc-macro-hack macro nor calls to any other proc-macro-hack macros. Use
117  [`proc-macro-nested`] if you require support for nested invocations.
118
119- By default, hygiene is structured such that the expanded code can't refer to
120  local variables other than those passed by name somewhere in the macro input.
121  If your macro must refer to *local* variables that don't get named in the
122  macro input, use `#[proc_macro_hack(fake_call_site)]` on the re-export in your
123  declaration crate. *Most macros won't need this.*
124
125[#10]: https://github.com/dtolnay/proc-macro-hack/issues/10
126[#20]: https://github.com/dtolnay/proc-macro-hack/issues/20
127[`proc-macro-nested`]: https://docs.rs/proc-macro-nested
128
129<br>
130
131#### License
132
133<sup>
134Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
1352.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
136</sup>
137
138<br>
139
140<sub>
141Unless you explicitly state otherwise, any contribution intentionally submitted
142for inclusion in this hack by you, as defined in the Apache-2.0 license, shall
143be dual licensed as above, without any additional terms or conditions.
144</sub>
145