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 |
| 20 | the real `proc_macro` crate once the support on nightly is stabilize. |
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] |
| 30 | proc-macro2 = "0.1" |
| 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 | |
| 51 | If you'd like you can enable the `unstable` feature in this crate. This will |
| 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] |
| 60 | proc-macro2 = { version = "0.1", features = ["unstable"] } |
| 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 |
| 68 | `proc_macro` stabilizations. These features are not exported by default. |
| 69 | |
| 70 | To export these features, the `procmacro2_unstable` config flag must be passed |
| 71 | to rustc. To pass this flag, run `cargo` with |
| 72 | `RUSTFLAGS='--cfg procmacro2_unstable' cargo build`. |
| 73 | |
| 74 | |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 75 | # License |
| 76 | |
Alex Crichton | a5aa9b9 | 2017-10-30 14:10:07 -0700 | [diff] [blame] | 77 | This project is licensed under either of |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 78 | |
Alex Crichton | 6dda542 | 2017-10-30 14:07:37 -0700 | [diff] [blame] | 79 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 80 | http://www.apache.org/licenses/LICENSE-2.0) |
| 81 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 82 | http://opensource.org/licenses/MIT) |
| 83 | |
| 84 | at your option. |
| 85 | |
| 86 | ### Contribution |
| 87 | |
| 88 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 89 | for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be |
| 90 | dual licensed as above, without any additional terms or conditions. |