charon_lib/ids/
mod.rs

1pub mod generator;
2pub mod vector;
3
4pub use generator::Generator;
5pub use vector::Vector;
6
7/// Generate an `Index` index type. We use it because we need manipulate a lot of different indices
8/// (for various kinds of declarations, variables, blocks, etc.).
9/// For sanity, we prevent any confusion between the different kinds of indices by using different
10/// types. The following macro allows us to easily derive those types.
11///
12/// The `name` parameter should contain the name of the module to declare. The `pretty_name`
13/// parameter is used to implement `Id::to_pretty_string`; if not provided, it defaults to `name`.
14#[macro_export]
15macro_rules! generate_index_type {
16    ($name:ident) => {
17        $crate::generate_index_type!($name, stringify!($name));
18    };
19    ($name:ident, $pretty_name:expr) => {
20        index_vec::define_index_type! {
21            #[derive(derive_generic_visitor::Drive, derive_generic_visitor::DriveMut)]
22            #[drive(skip)]
23            pub struct $name = usize;
24            // Must fit in an u32 for serialization.
25            MAX_INDEX = std::u32::MAX as usize;
26        }
27
28        impl $name {
29            pub const ZERO: Self = Self { _raw: 0 };
30            pub fn is_zero(&self) -> bool {
31                self.index() == 0
32            }
33            pub fn to_pretty_string(self) -> String {
34                format!("@{}{}", $pretty_name, self)
35            }
36        }
37
38        impl std::fmt::Display for $name {
39            fn fmt(
40                &self,
41                f: &mut std::fmt::Formatter<'_>,
42            ) -> std::result::Result<(), std::fmt::Error> {
43                f.write_str(self.index().to_string().as_str())
44            }
45        }
46    };
47}