ra_ap_rustc_index_macros/lib.rs
1// tidy-alphabetical-start
2#![cfg_attr(feature = "nightly", allow(internal_features))]
3#![cfg_attr(feature = "nightly", feature(allow_internal_unstable))]
4// tidy-alphabetical-end
5
6use proc_macro::TokenStream;
7
8mod newtype;
9
10/// Creates a struct type `S` that can be used as an index with
11/// `IndexVec` and so on.
12///
13/// There are two ways of interacting with these indices:
14///
15/// - The `From` impls are the preferred way. So you can do
16/// `S::from(v)` with a `usize` or `u32`. And you can convert back
17/// to an integer with `u32::from(s)`.
18///
19/// - Alternatively, you can use the methods `S::new(v)` and `s.index()`
20/// to create/return a value.
21///
22/// Internally, the index uses a u32, so the index must not exceed
23/// `u32::MAX`.
24///
25/// The impls provided by default are Clone, Copy, PartialEq, Eq, and Hash.
26///
27/// Accepted attributes for customization:
28/// - `#[derive(HashStable_Generic)]`/`#[derive(HashStable)]`: derives
29/// `HashStable`, as normal.
30/// - `#[encodable]`: derives `Encodable`/`Decodable`.
31/// - `#[orderable]`: derives `PartialOrd`/`Ord`, plus step-related methods.
32/// - `#[debug_format = "Foo({})"]`: derives `Debug` with particular output.
33/// - `#[max = 0xFFFF_FFFD]`: specifies the max value, which allows niche
34/// optimizations. The default max value is 0xFFFF_FF00.
35/// - `#[gate_rustc_only]`: makes parts of the generated code nightly-only.
36#[proc_macro]
37#[cfg_attr(feature = "nightly", allow_internal_unstable(step_trait, rustc_attrs, trusted_step))]
38pub fn newtype_index(input: TokenStream) -> TokenStream {
39 newtype::newtype(input)
40}