blob: c26ec750200eeab6f7fc507f15c43bc9adfa42c6 [file] [log] [blame] [view]
Alex Crichton803720f2017-05-19 19:41:03 -07001# proc-macro2
2
David Tolnayb10733f2017-12-31 12:22:37 -05003[![Build Status](https://api.travis-ci.org/alexcrichton/proc-macro2.svg?branch=master)](https://travis-ci.org/alexcrichton/proc-macro2)
4[![Latest Version](https://img.shields.io/crates/v/proc-macro2.svg)](https://crates.io/crates/proc-macro2)
5[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/proc-macro2)
Alex Crichton803720f2017-05-19 19:41:03 -07006
Alex Crichton1bc895e2017-07-05 17:53:06 -07007A small shim over the `proc_macro` crate in the compiler intended to multiplex
8the current stable interface (as of 2017-07-05) and the [upcoming richer
Alex Crichton803720f2017-05-19 19:41:03 -07009interface][upcoming].
10
11[upcoming]: https://github.com/rust-lang/rust/pull/40939
12
13The 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 Crichton1bc895e2017-07-05 17:53:06 -070019The hope is that libraries ported to `proc_macro2` will be trivial to port to
20the real `proc_macro` crate once the support on nightly is stabilize.
Alex Crichton803720f2017-05-19 19:41:03 -070021
Alex Crichton1bc895e2017-07-05 17:53:06 -070022## Usage
23
24This crate by default compiles on the stable version of the compiler. It only
25uses the stable surface area of the `proc_macro` crate upstream in the compiler
26itself. Usage is done via:
27
28```toml
29[dependencies]
30proc-macro2 = "0.1"
31```
32
33followed by
34
35```rust
36extern crate proc_macro;
37extern crate proc_macro2;
38
39#[proc_macro_derive(MyDerive)]
40pub 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
51If you'd like you can enable the `unstable` feature in this crate. This will
52cause 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
54preserve span information, however, coming in from the compiler itself.
55
56You can enable this feature via:
57
58```toml
59[dependencies]
60proc-macro2 = { version = "0.1", features = ["unstable"] }
61```
Alex Crichton803720f2017-05-19 19:41:03 -070062
Alex Crichton6dda5422017-10-30 14:07:37 -070063
Nika Layzell34636742017-12-30 14:56:58 -050064## Unstable Features
65
66`proc-macro2` supports exporting some methods from `proc_macro` which are
67currently highly unstable, and may not be stabilized in the first pass of
68`proc_macro` stabilizations. These features are not exported by default.
69
70To export these features, the `procmacro2_unstable` config flag must be passed
71to rustc. To pass this flag, run `cargo` with
72`RUSTFLAGS='--cfg procmacro2_unstable' cargo build`.
73
74
Alex Crichton803720f2017-05-19 19:41:03 -070075# License
76
Alex Crichtona5aa9b92017-10-30 14:10:07 -070077This project is licensed under either of
Alex Crichton803720f2017-05-19 19:41:03 -070078
Alex Crichton6dda5422017-10-30 14:07:37 -070079 * 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
84at your option.
85
86### Contribution
87
88Unless you explicitly state otherwise, any contribution intentionally submitted
89for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be
90dual licensed as above, without any additional terms or conditions.