blob: 6a9153f86e691b5d1814b897f86f44ce5afa793e [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]
Alex Crichton2794f102018-03-31 02:41:31 -070030proc-macro2 = "0.3"
Alex Crichton1bc895e2017-07-05 17:53:06 -070031```
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
David Tolnayd66ecf62018-01-02 20:05:42 -080051If you'd like you can enable the `nightly` feature in this crate. This will
Alex Crichton1bc895e2017-07-05 17:53:06 -070052cause 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]
Alex Crichton2794f102018-03-31 02:41:31 -070060proc-macro2 = { version = "0.3", features = ["nightly"] }
Alex Crichton1bc895e2017-07-05 17:53:06 -070061```
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
David Tolnay1ebe3972018-01-02 20:14:20 -080068`proc_macro` stabilizations. These features are not exported by default. Minor
69versions of `proc-macro2` may make breaking changes to them at any time.
Nika Layzell34636742017-12-30 14:56:58 -050070
David Tolnay1ebe3972018-01-02 20:14:20 -080071To enable these features, the `procmacro2_semver_exempt` config flag must be
72passed to rustc.
73
74```
75RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build
76```
77
78Note that this must not only be done for your crate, but for any crate that
79depends on your crate. This infectious nature is intentional, as it serves as a
80reminder that you are outside of the normal semver guarantees.
Nika Layzell34636742017-12-30 14:56:58 -050081
82
Alex Crichton803720f2017-05-19 19:41:03 -070083# License
84
Alex Crichtona5aa9b92017-10-30 14:10:07 -070085This project is licensed under either of
Alex Crichton803720f2017-05-19 19:41:03 -070086
Alex Crichton6dda5422017-10-30 14:07:37 -070087 * 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
92at your option.
93
94### Contribution
95
96Unless you explicitly state otherwise, any contribution intentionally submitted
97for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be
98dual licensed as above, without any additional terms or conditions.