Skip to main content

hax_rust_engine/
symbol.rs

1//! Interned string identifiers used throughout the AST.
2//!
3//! Symbols are lightweight wrappers around `String` for use in identifiers.
4//! Eventually, this could be backed by a real interner or arena.
5
6use std::ops::Deref;
7
8use hax_rust_engine_macros::*;
9
10/// Interned string identifier for the AST
11#[derive_group_for_ast]
12pub struct Symbol(String);
13
14impl Symbol {
15    /// Create a new symbol
16    pub fn new(s: impl AsRef<str>) -> Self {
17        Self(s.as_ref().to_string())
18    }
19}
20
21impl Deref for Symbol {
22    type Target = str;
23
24    fn deref(&self) -> &Self::Target {
25        &self.0
26    }
27}
28
29impl AsRef<str> for Symbol {
30    fn as_ref(&self) -> &str {
31        &self.0
32    }
33}
34
35impl std::fmt::Display for Symbol {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
37        write!(f, "{}", self.0)
38    }
39}