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)]`: derives `HashStable`, as normal.
29/// - `#[encodable]`: derives `Encodable`/`Decodable`.
30/// - `#[orderable]`: derives `PartialOrd`/`Ord`, plus step-related methods.
31/// - `#[debug_format = "Foo({})"]`: derives `Debug` with particular output.
32/// - `#[max = 0xFFFF_FFFD]`: specifies the max value, which allows niche
33/// optimizations. The default max value is 0xFFFF_FF00.
34/// - `#[gate_rustc_only]`: makes parts of the generated code nightly-only.
35#[proc_macro]
36#[cfg_attr(
37 feature = "nightly",
38 allow_internal_unstable(
39 step_trait,
40 rustc_attrs,
41 trusted_step,
42 pattern_types,
43 pattern_type_macro,
44 structural_match,
45 )
46)]
47pub fn newtype_index(input: TokenStream) -> TokenStream {
48 newtype::newtype(input)
49}