Skip to main content

airl_ir/
ids.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4macro_rules! define_id {
5    ($(#[$doc:meta])* $name:ident) => {
6        $(#[$doc])*
7        #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
8        #[serde(transparent)]
9        #[allow(missing_docs)]
10        pub struct $name(pub String);
11
12        impl $name {
13            /// Construct a new ID from any string-like value.
14            pub fn new(s: impl Into<String>) -> Self {
15                Self(s.into())
16            }
17
18            /// Get the underlying string slice.
19            pub fn as_str(&self) -> &str {
20                &self.0
21            }
22        }
23
24        impl fmt::Display for $name {
25            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26                write!(f, "{}", self.0)
27            }
28        }
29
30        impl From<&str> for $name {
31            fn from(s: &str) -> Self {
32                Self(s.to_string())
33            }
34        }
35
36        impl From<String> for $name {
37            fn from(s: String) -> Self {
38                Self(s)
39            }
40        }
41    };
42}
43
44define_id!(
45    /// Unique identifier for an IR node.
46    NodeId
47);
48define_id!(
49    /// Unique identifier for a type definition.
50    TypeId
51);
52define_id!(
53    /// Unique identifier for a function.
54    FuncId
55);
56define_id!(
57    /// Unique identifier for a module.
58    ModuleId
59);
60define_id!(
61    /// Symbol (interned string) used for names and identifiers.
62    Symbol
63);