Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 1 | # proc-macro2 |
| 2 | |
David Tolnay | b10733f | 2017-12-31 12:22:37 -0500 | [diff] [blame] | 3 | [](https://travis-ci.org/alexcrichton/proc-macro2) |
| 4 | [](https://crates.io/crates/proc-macro2) |
| 5 | [](https://docs.rs/proc-macro2) |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 6 | |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 7 | A small shim over the `proc_macro` crate in the compiler intended to multiplex |
| 8 | the current stable interface (as of 2017-07-05) and the [upcoming richer |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 9 | interface][upcoming]. |
| 10 | |
| 11 | [upcoming]: https://github.com/rust-lang/rust/pull/40939 |
| 12 | |
| 13 | The upcoming support has features like: |
| 14 | |
| 15 | * Span information on tokens |
| 16 | * No need to go in/out through strings |
| 17 | * Structured input/output |
| 18 | |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 19 | The hope is that libraries ported to `proc_macro2` will be trivial to port to |
David Tolnay | 49302e7 | 2018-04-25 21:23:18 -0700 | [diff] [blame^] | 20 | the real `proc_macro` crate once the support on nightly is stabilized. |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 21 | |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 22 | ## Usage |
| 23 | |
| 24 | This crate by default compiles on the stable version of the compiler. It only |
| 25 | uses the stable surface area of the `proc_macro` crate upstream in the compiler |
| 26 | itself. Usage is done via: |
| 27 | |
| 28 | ```toml |
| 29 | [dependencies] |
Alex Crichton | 2794f10 | 2018-03-31 02:41:31 -0700 | [diff] [blame] | 30 | proc-macro2 = "0.3" |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 31 | ``` |
| 32 | |
| 33 | followed by |
| 34 | |
| 35 | ```rust |
| 36 | extern crate proc_macro; |
| 37 | extern crate proc_macro2; |
| 38 | |
| 39 | #[proc_macro_derive(MyDerive)] |
| 40 | pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 41 | let input: proc_macro2::TokenStream = input.into(); |
| 42 | |
| 43 | let output: proc_macro2::TokenStream = { |
| 44 | /* transform input */ |
| 45 | }; |
| 46 | |
| 47 | output.into() |
| 48 | } |
| 49 | ``` |
| 50 | |
David Tolnay | d66ecf6 | 2018-01-02 20:05:42 -0800 | [diff] [blame] | 51 | If you'd like you can enable the `nightly` feature in this crate. This will |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 52 | cause it to compile against the **unstable and nightly-only** features of the |
| 53 | `proc_macro` crate. This in turn requires a nightly compiler. This should help |
| 54 | preserve span information, however, coming in from the compiler itself. |
| 55 | |
| 56 | You can enable this feature via: |
| 57 | |
| 58 | ```toml |
| 59 | [dependencies] |
Alex Crichton | 2794f10 | 2018-03-31 02:41:31 -0700 | [diff] [blame] | 60 | proc-macro2 = { version = "0.3", features = ["nightly"] } |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 61 | ``` |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 62 | |
Alex Crichton | 6dda542 | 2017-10-30 14:07:37 -0700 | [diff] [blame] | 63 | |
Nika Layzell | 3463674 | 2017-12-30 14:56:58 -0500 | [diff] [blame] | 64 | ## Unstable Features |
| 65 | |
| 66 | `proc-macro2` supports exporting some methods from `proc_macro` which are |
| 67 | currently highly unstable, and may not be stabilized in the first pass of |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 68 | `proc_macro` stabilizations. These features are not exported by default. Minor |
| 69 | versions of `proc-macro2` may make breaking changes to them at any time. |
Nika Layzell | 3463674 | 2017-12-30 14:56:58 -0500 | [diff] [blame] | 70 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 71 | To enable these features, the `procmacro2_semver_exempt` config flag must be |
| 72 | passed to rustc. |
| 73 | |
| 74 | ``` |
| 75 | RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build |
| 76 | ``` |
| 77 | |
| 78 | Note that this must not only be done for your crate, but for any crate that |
| 79 | depends on your crate. This infectious nature is intentional, as it serves as a |
| 80 | reminder that you are outside of the normal semver guarantees. |
Nika Layzell | 3463674 | 2017-12-30 14:56:58 -0500 | [diff] [blame] | 81 | |
| 82 | |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 83 | # License |
| 84 | |
Alex Crichton | a5aa9b9 | 2017-10-30 14:10:07 -0700 | [diff] [blame] | 85 | This project is licensed under either of |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 86 | |
Alex Crichton | 6dda542 | 2017-10-30 14:07:37 -0700 | [diff] [blame] | 87 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 88 | http://www.apache.org/licenses/LICENSE-2.0) |
| 89 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 90 | http://opensource.org/licenses/MIT) |
| 91 | |
| 92 | at your option. |
| 93 | |
| 94 | ### Contribution |
| 95 | |
| 96 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 97 | for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be |
| 98 | dual licensed as above, without any additional terms or conditions. |