17ac06127Sopenharmony_ci# proc-macro2
27ac06127Sopenharmony_ci
37ac06127Sopenharmony_ci[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/proc--macro2-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/proc-macro2)
47ac06127Sopenharmony_ci[<img alt="crates.io" src="https://img.shields.io/crates/v/proc-macro2.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/proc-macro2)
57ac06127Sopenharmony_ci[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-proc--macro2-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/proc-macro2)
67ac06127Sopenharmony_ci[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/dtolnay/proc-macro2/ci.yml?branch=master&style=for-the-badge" height="20">](https://github.com/dtolnay/proc-macro2/actions?query=branch%3Amaster)
77ac06127Sopenharmony_ci
87ac06127Sopenharmony_ciA wrapper around the procedural macro API of the compiler's `proc_macro` crate.
97ac06127Sopenharmony_ciThis library serves two purposes:
107ac06127Sopenharmony_ci
117ac06127Sopenharmony_ci- **Bring proc-macro-like functionality to other contexts like build.rs and
127ac06127Sopenharmony_ci  main.rs.** Types from `proc_macro` are entirely specific to procedural macros
137ac06127Sopenharmony_ci  and cannot ever exist in code outside of a procedural macro. Meanwhile
147ac06127Sopenharmony_ci  `proc_macro2` types may exist anywhere including non-macro code. By developing
157ac06127Sopenharmony_ci  foundational libraries like [syn] and [quote] against `proc_macro2` rather
167ac06127Sopenharmony_ci  than `proc_macro`, the procedural macro ecosystem becomes easily applicable to
177ac06127Sopenharmony_ci  many other use cases and we avoid reimplementing non-macro equivalents of
187ac06127Sopenharmony_ci  those libraries.
197ac06127Sopenharmony_ci
207ac06127Sopenharmony_ci- **Make procedural macros unit testable.** As a consequence of being specific
217ac06127Sopenharmony_ci  to procedural macros, nothing that uses `proc_macro` can be executed from a
227ac06127Sopenharmony_ci  unit test. In order for helper libraries or components of a macro to be
237ac06127Sopenharmony_ci  testable in isolation, they must be implemented using `proc_macro2`.
247ac06127Sopenharmony_ci
257ac06127Sopenharmony_ci[syn]: https://github.com/dtolnay/syn
267ac06127Sopenharmony_ci[quote]: https://github.com/dtolnay/quote
277ac06127Sopenharmony_ci
287ac06127Sopenharmony_ci## Usage
297ac06127Sopenharmony_ci
307ac06127Sopenharmony_ci```toml
317ac06127Sopenharmony_ci[dependencies]
327ac06127Sopenharmony_ciproc-macro2 = "1.0"
337ac06127Sopenharmony_ci```
347ac06127Sopenharmony_ci
357ac06127Sopenharmony_ciThe skeleton of a typical procedural macro typically looks like this:
367ac06127Sopenharmony_ci
377ac06127Sopenharmony_ci```rust
387ac06127Sopenharmony_ciextern crate proc_macro;
397ac06127Sopenharmony_ci
407ac06127Sopenharmony_ci#[proc_macro_derive(MyDerive)]
417ac06127Sopenharmony_cipub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
427ac06127Sopenharmony_ci    let input = proc_macro2::TokenStream::from(input);
437ac06127Sopenharmony_ci
447ac06127Sopenharmony_ci    let output: proc_macro2::TokenStream = {
457ac06127Sopenharmony_ci        /* transform input */
467ac06127Sopenharmony_ci    };
477ac06127Sopenharmony_ci
487ac06127Sopenharmony_ci    proc_macro::TokenStream::from(output)
497ac06127Sopenharmony_ci}
507ac06127Sopenharmony_ci```
517ac06127Sopenharmony_ci
527ac06127Sopenharmony_ciIf parsing with [Syn], you'll use [`parse_macro_input!`] instead to propagate
537ac06127Sopenharmony_ciparse errors correctly back to the compiler when parsing fails.
547ac06127Sopenharmony_ci
557ac06127Sopenharmony_ci[`parse_macro_input!`]: https://docs.rs/syn/2.0/syn/macro.parse_macro_input.html
567ac06127Sopenharmony_ci
577ac06127Sopenharmony_ci## Unstable features
587ac06127Sopenharmony_ci
597ac06127Sopenharmony_ciThe default feature set of proc-macro2 tracks the most recent stable compiler
607ac06127Sopenharmony_ciAPI. Functionality in `proc_macro` that is not yet stable is not exposed by
617ac06127Sopenharmony_ciproc-macro2 by default.
627ac06127Sopenharmony_ci
637ac06127Sopenharmony_ciTo opt into the additional APIs available in the most recent nightly compiler,
647ac06127Sopenharmony_cithe `procmacro2_semver_exempt` config flag must be passed to rustc. We will
657ac06127Sopenharmony_cipolyfill those nightly-only APIs back to Rust 1.56.0. As these are unstable APIs
667ac06127Sopenharmony_cithat track the nightly compiler, minor versions of proc-macro2 may make breaking
677ac06127Sopenharmony_cichanges to them at any time.
687ac06127Sopenharmony_ci
697ac06127Sopenharmony_ci```
707ac06127Sopenharmony_ciRUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build
717ac06127Sopenharmony_ci```
727ac06127Sopenharmony_ci
737ac06127Sopenharmony_ciNote that this must not only be done for your crate, but for any crate that
747ac06127Sopenharmony_cidepends on your crate. This infectious nature is intentional, as it serves as a
757ac06127Sopenharmony_cireminder that you are outside of the normal semver guarantees.
767ac06127Sopenharmony_ci
777ac06127Sopenharmony_ciSemver exempt methods are marked as such in the proc-macro2 documentation.
787ac06127Sopenharmony_ci
797ac06127Sopenharmony_ci<br>
807ac06127Sopenharmony_ci
817ac06127Sopenharmony_ci#### License
827ac06127Sopenharmony_ci
837ac06127Sopenharmony_ci<sup>
847ac06127Sopenharmony_ciLicensed under either of <a href="LICENSE-APACHE">Apache License, Version
857ac06127Sopenharmony_ci2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
867ac06127Sopenharmony_ci</sup>
877ac06127Sopenharmony_ci
887ac06127Sopenharmony_ci<br>
897ac06127Sopenharmony_ci
907ac06127Sopenharmony_ci<sub>
917ac06127Sopenharmony_ciUnless you explicitly state otherwise, any contribution intentionally submitted
927ac06127Sopenharmony_cifor inclusion in this crate by you, as defined in the Apache-2.0 license, shall
937ac06127Sopenharmony_cibe dual licensed as above, without any additional terms or conditions.
947ac06127Sopenharmony_ci</sub>
95