1
Fork 0

Convert newtype_index to a proc macro

The `macro_rules!` implementation was becomng excessively complicated,
and difficult to modify. The new proc macro implementation should make
it much easier to add new features (e.g. skipping certain `#[derive]`s)
This commit is contained in:
Aaron Hill 2022-02-09 17:24:51 -05:00
parent 4b043faba3
commit 339bbebbc1
No known key found for this signature in database
GPG key ID: B4087E510E98B164
4 changed files with 349 additions and 455 deletions

View file

@ -1,4 +1,5 @@
#![feature(proc_macro_diagnostic)]
#![feature(allow_internal_unstable)]
#![allow(rustc::default_hash_types)]
#![recursion_limit = "128"]
@ -8,6 +9,7 @@ use proc_macro::TokenStream;
mod hash_stable;
mod lift;
mod newtype;
mod query;
mod serialize;
mod session_diagnostic;
@ -24,6 +26,27 @@ pub fn symbols(input: TokenStream) -> TokenStream {
symbols::symbols(input.into()).into()
}
/// Creates a struct type `S` that can be used as an index with
/// `IndexVec` and so on.
///
/// There are two ways of interacting with these indices:
///
/// - The `From` impls are the preferred way. So you can do
/// `S::from(v)` with a `usize` or `u32`. And you can convert back
/// to an integer with `u32::from(s)`.
///
/// - Alternatively, you can use the methods `S::new(v)` and `s.index()`
/// to create/return a value.
///
/// Internally, the index uses a u32, so the index must not exceed
/// `u32::MAX`. You can also customize things like the `Debug` impl,
/// what traits are derived, and so forth via the macro.
#[proc_macro]
#[allow_internal_unstable(step_trait, rustc_attrs, trusted_step)]
pub fn newtype_index(input: TokenStream) -> TokenStream {
newtype::newtype(input).into()
}
decl_derive!([HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive);
decl_derive!(
[HashStable_Generic, attributes(stable_hasher)] =>