blob: a31c0523986e7eab954f8ce7a1aa0ec9060f966f [file] [log] [blame] [view]
Alex Crichton803720f2017-05-19 19:41:03 -07001# proc-macro2
2
Alex Crichton1bc895e2017-07-05 17:53:06 -07003[Documentation](https://docs.rs/proc-macro2)
Alex Crichton803720f2017-05-19 19:41:03 -07004
Alex Crichton1bc895e2017-07-05 17:53:06 -07005A small shim over the `proc_macro` crate in the compiler intended to multiplex
6the current stable interface (as of 2017-07-05) and the [upcoming richer
Alex Crichton803720f2017-05-19 19:41:03 -07007interface][upcoming].
8
9[upcoming]: https://github.com/rust-lang/rust/pull/40939
10
11The 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 Crichton1bc895e2017-07-05 17:53:06 -070017The hope is that libraries ported to `proc_macro2` will be trivial to port to
18the real `proc_macro` crate once the support on nightly is stabilize.
Alex Crichton803720f2017-05-19 19:41:03 -070019
Alex Crichton1bc895e2017-07-05 17:53:06 -070020## Usage
21
22This crate by default compiles on the stable version of the compiler. It only
23uses the stable surface area of the `proc_macro` crate upstream in the compiler
24itself. Usage is done via:
25
26```toml
27[dependencies]
28proc-macro2 = "0.1"
29```
30
31followed by
32
33```rust
34extern crate proc_macro;
35extern crate proc_macro2;
36
37#[proc_macro_derive(MyDerive)]
38pub 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
49If you'd like you can enable the `unstable` feature in this crate. This will
50cause 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
52preserve span information, however, coming in from the compiler itself.
53
54You can enable this feature via:
55
56```toml
57[dependencies]
58proc-macro2 = { version = "0.1", features = ["unstable"] }
59```
Alex Crichton803720f2017-05-19 19:41:03 -070060
61# License
62
63`proc-macro2` is primarily distributed under the terms of both the MIT license and
64the Apache License (Version 2.0), with portions covered by various BSD-like
65licenses.
66
67See LICENSE-APACHE, and LICENSE-MIT for details.