|
| 1 | +//! Pre-interned [`Symbol`]s available in `const` contexts. |
| 2 | +//! |
| 3 | +//! [`Symbol`]s are [interned strings](https://en.wikipedia.org/wiki/String_interning) that are |
| 4 | +//! cheap to store and compare. This module contains a list of pre-interned symbol constants that |
| 5 | +//! are used in the linter. The only exception to this is the [`EXTRA_SYMBOLS`] constant, which |
| 6 | +//! contains a complete ordered list of all symbols to be pre-interned. |
| 7 | +
|
| 8 | +use rustc_span::{Symbol, symbol::PREDEFINED_SYMBOLS_COUNT}; |
| 9 | + |
| 10 | +/// A helper used by [`declare_bevy_symbols!`] to extract its input. |
| 11 | +/// |
| 12 | +/// ``` |
| 13 | +/// assert_eq!(extract_value!(Name), "Name"); |
| 14 | +/// assert_eq!(extract_value!(Name: "value"), "value"); |
| 15 | +/// ``` |
| 16 | +macro_rules! extract_value { |
| 17 | + ($name:ident) => { |
| 18 | + stringify!($name) |
| 19 | + }; |
| 20 | + ($name:ident: $value:literal) => { |
| 21 | + $value |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +/// Generates the [`Symbol`] constants and [`EXTRA_SYMBOLS`] from a list of name-value pairs. |
| 26 | +/// |
| 27 | +/// # Example |
| 28 | +/// |
| 29 | +/// ``` |
| 30 | +/// declare_bevy_symbols! { |
| 31 | +/// // Interns the string "Hello, world" available as the constant named `Hello`. |
| 32 | +/// Hello: "Hello, world!", |
| 33 | +/// // Interns the string "bevy" available as the constant named `bevy`. This is the shorthand! |
| 34 | +/// bevy, |
| 35 | +/// } |
| 36 | +/// ``` |
| 37 | +macro_rules! declare_bevy_symbols { |
| 38 | + { |
| 39 | + $($name:ident $(: $value:literal)?),* $(,)? |
| 40 | + } => { |
| 41 | + /// A list of strings that are pre-interned at the beginning of linting through |
| 42 | + /// [`Config::extra_symbols`](rustc_interface::interface::Config::extra_symbols). |
| 43 | + pub const EXTRA_SYMBOLS: &[&str] = &[ |
| 44 | + $( |
| 45 | + extract_value!($name $(: $value)?) |
| 46 | + ),* |
| 47 | + ]; |
| 48 | + |
| 49 | + $( |
| 50 | + #[doc = concat!("A pre-interned [`Symbol`] for the string \"", extract_value!($name $(: $value)?), "\".")] |
| 51 | + pub const $name: Symbol = Symbol::new(PREDEFINED_SYMBOLS_COUNT + ${index()}); |
| 52 | + )* |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +declare_bevy_symbols! {} |
0 commit comments