Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 1 | # proc-macro2 |
| 2 | |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 3 | [Documentation](https://docs.rs/proc-macro2) |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 4 | |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 5 | A small shim over the `proc_macro` crate in the compiler intended to multiplex |
| 6 | 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] | 7 | interface][upcoming]. |
| 8 | |
| 9 | [upcoming]: https://github.com/rust-lang/rust/pull/40939 |
| 10 | |
| 11 | The upcoming support has features like: |
| 12 | |
| 13 | * Span information on tokens |
| 14 | * No need to go in/out through strings |
| 15 | * Structured input/output |
| 16 | |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 17 | The hope is that libraries ported to `proc_macro2` will be trivial to port to |
| 18 | the real `proc_macro` crate once the support on nightly is stabilize. |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 19 | |
Alex Crichton | 1bc895e | 2017-07-05 17:53:06 -0700 | [diff] [blame] | 20 | ## Usage |
| 21 | |
| 22 | This crate by default compiles on the stable version of the compiler. It only |
| 23 | uses the stable surface area of the `proc_macro` crate upstream in the compiler |
| 24 | itself. Usage is done via: |
| 25 | |
| 26 | ```toml |
| 27 | [dependencies] |
| 28 | proc-macro2 = "0.1" |
| 29 | ``` |
| 30 | |
| 31 | followed by |
| 32 | |
| 33 | ```rust |
| 34 | extern crate proc_macro; |
| 35 | extern crate proc_macro2; |
| 36 | |
| 37 | #[proc_macro_derive(MyDerive)] |
| 38 | pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 39 | let input: proc_macro2::TokenStream = input.into(); |
| 40 | |
| 41 | let output: proc_macro2::TokenStream = { |
| 42 | /* transform input */ |
| 43 | }; |
| 44 | |
| 45 | output.into() |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | If you'd like you can enable the `unstable` feature in this crate. This will |
| 50 | cause it to compile against the **unstable and nightly-only** features of the |
| 51 | `proc_macro` crate. This in turn requires a nightly compiler. This should help |
| 52 | preserve span information, however, coming in from the compiler itself. |
| 53 | |
| 54 | You can enable this feature via: |
| 55 | |
| 56 | ```toml |
| 57 | [dependencies] |
| 58 | proc-macro2 = { version = "0.1", features = ["unstable"] } |
| 59 | ``` |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 60 | |
Alex Crichton | 6dda542 | 2017-10-30 14:07:37 -0700 | [diff] [blame^] | 61 | |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 62 | # License |
| 63 | |
Alex Crichton | 6dda542 | 2017-10-30 14:07:37 -0700 | [diff] [blame^] | 64 | Serde is licensed under either of |
Alex Crichton | 803720f | 2017-05-19 19:41:03 -0700 | [diff] [blame] | 65 | |
Alex Crichton | 6dda542 | 2017-10-30 14:07:37 -0700 | [diff] [blame^] | 66 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 67 | http://www.apache.org/licenses/LICENSE-2.0) |
| 68 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 69 | http://opensource.org/licenses/MIT) |
| 70 | |
| 71 | at your option. |
| 72 | |
| 73 | ### Contribution |
| 74 | |
| 75 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 76 | for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be |
| 77 | dual licensed as above, without any additional terms or conditions. |