numbered_macros/lib.rs
1//! Proc-macro implementation for [`numbered`](https://docs.rs/numbered).
2//!
3//! Depend on the `numbered` crate, not this one. This crate is the host
4//! proc-macro; generated items use `core` so the runtime can be `no_std`.
5
6mod numbered;
7
8use proc_macro::TokenStream;
9
10/// Derive stable integer numbers for an enum.
11///
12/// # Container attribute
13///
14/// ```ignore
15/// #[derive(Numbered)]
16/// #[numbered(u8, start = 0, crate = ::numbered)]
17/// enum Kind { Process }
18/// ```
19///
20/// - A repr type (required): `u8`, `u16`, `u32`, `u64`, `usize`, `i8`,
21/// `i16`, `i32`, `i64`, or `isize`. First positional, or `repr = u8`.
22/// - `start = N`: first auto-assigned number when no discriminant / `n` is
23/// set. Default: `0`.
24/// - `crate = ::path`: crate path emitted in generated code. Default:
25/// `::numbered`. Set this when you re-export numbered from another crate.
26///
27/// # Variant attribute
28///
29/// ```ignore
30/// #[numbered(n = 10)]
31/// Network,
32/// ```
33///
34/// Pins that variant to an explicit integer. A native `= 10` discriminant
35/// does the same. If both are present they must agree.
36///
37/// # Generated items
38///
39/// All of these are trait impls. Nothing is inherent on `E`.
40///
41/// - `numbered::Number`: `number` / `as_u8` (`as_*` follows the repr).
42/// Not `const` (trait methods cannot be)
43/// - `numbered::FromNumber`: `from_number` / `from_u8` (fieldless enums).
44/// Not `const`
45/// - `numbered::Variants` (non-generic enums): `VARIANTS` / `NUMBERS` /
46/// `COUNT`. Fielded enums use `Variant = ()` so `VARIANTS.len()` still
47/// works next to cognomen extras. Const use goes through `NUMBERS`
48/// - `From<Self> for <repr>`, `TryFrom<repr> for Self`
49/// - `PartialEq<repr>` both ways
50/// - `Serialize` / `Deserialize` (feature `serde`)
51///
52/// See the [`numbered`](https://docs.rs/numbered) crate docs for assignment
53/// rules, features, and `no_std`.
54#[proc_macro_derive(Numbered, attributes(numbered))]
55pub fn derive_numbered(input: TokenStream) -> TokenStream {
56 numbered::derive(input.into())
57 .unwrap_or_else(|e| e.to_compile_error())
58 .into()
59}