1
0
dust/dust-lang/src/identifier.rs

102 lines
2.3 KiB
Rust
Raw Normal View History

2024-03-25 04:16:55 +00:00
use std::{
2024-06-24 14:46:37 +00:00
collections::HashMap,
2024-03-25 04:16:55 +00:00
fmt::{self, Display, Formatter},
2024-06-24 14:46:37 +00:00
sync::{Arc, OnceLock, RwLock},
2024-03-25 04:16:55 +00:00
};
2024-06-04 18:47:15 +00:00
use serde::{de::Visitor, Deserialize, Serialize};
2024-06-24 14:46:37 +00:00
static IDENTIFIER_CACHE: OnceLock<RwLock<HashMap<String, Identifier>>> = OnceLock::new();
fn identifier_cache<'a>() -> &'a RwLock<HashMap<String, Identifier>> {
IDENTIFIER_CACHE.get_or_init(|| RwLock::new(HashMap::new()))
}
2024-03-25 04:16:55 +00:00
#[derive(Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub struct Identifier(Arc<String>);
impl Identifier {
2024-06-24 14:46:37 +00:00
pub fn new(string: &str) -> Self {
let cache = identifier_cache();
if let Some(identifier) = cache.read().unwrap().get(string) {
return identifier.clone();
}
let identifier = Identifier(Arc::new(string.to_string()));
cache
.write()
.unwrap()
.insert(string.to_string(), identifier.clone());
identifier
2024-03-25 04:16:55 +00:00
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl Display for Identifier {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
2024-06-04 18:47:15 +00:00
impl Serialize for Identifier {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.0.as_str())
}
}
impl<'de> Deserialize<'de> for Identifier {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
2024-06-19 08:56:56 +00:00
deserializer.deserialize_identifier(IdentifierVisitor)
2024-06-04 18:47:15 +00:00
}
}
2024-06-19 08:56:56 +00:00
struct IdentifierVisitor;
2024-06-04 18:47:15 +00:00
2024-06-19 08:56:56 +00:00
impl<'de> Visitor<'de> for IdentifierVisitor {
type Value = Identifier;
2024-06-04 18:47:15 +00:00
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a UTF-8 string")
}
2024-06-19 08:56:56 +00:00
fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str(v.encode_utf8(&mut [0u8; 4]))
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Identifier::new(v))
}
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str(v)
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str(&v)
}
2024-06-04 18:47:15 +00:00
}