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

108 lines
2.5 KiB
Rust
Raw Normal View History

2024-08-09 01:59:09 +00:00
//! Key used to identify a value or type.
2024-03-25 04:16:55 +00:00
use std::{
2024-08-08 18:58:12 +00:00
collections::HashSet,
2024-03-25 04:16:55 +00:00
fmt::{self, Display, Formatter},
hash::Hash,
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-08-08 18:58:12 +00:00
static IDENTIFIER_CACHE: OnceLock<RwLock<HashSet<Identifier>>> = OnceLock::new();
2024-06-24 14:46:37 +00:00
2024-08-08 18:58:12 +00:00
fn identifier_cache<'a>() -> &'a RwLock<HashSet<Identifier>> {
IDENTIFIER_CACHE.get_or_init(|| RwLock::new(HashSet::new()))
2024-06-24 14:46:37 +00:00
}
2024-08-09 01:59:09 +00:00
/// Key used to identify a value or type.
2024-07-04 18:39:45 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
2024-03-25 04:16:55 +00:00
pub struct Identifier(Arc<String>);
impl Identifier {
2024-08-08 18:58:12 +00:00
pub fn new<T: ToString>(text: T) -> Self {
2024-06-24 14:46:37 +00:00
let cache = identifier_cache();
2024-08-08 18:58:12 +00:00
let new = Identifier(Arc::new(text.to_string()));
if let Some(identifier) = cache.read().unwrap().get(&new).cloned() {
2024-07-04 18:39:45 +00:00
return identifier;
2024-06-24 14:46:37 +00:00
}
2024-08-08 18:58:12 +00:00
cache.write().unwrap().insert(new.clone());
2024-06-24 14:46:37 +00:00
2024-07-04 18:39:45 +00:00
new
2024-03-25 04:16:55 +00:00
}
pub fn as_str(&self) -> &str {
2024-07-12 14:20:52 +00:00
self.0.as_str()
2024-07-04 18:39:45 +00:00
}
}
impl From<&str> for Identifier {
fn from(text: &str) -> Self {
Identifier::new(text)
2024-03-25 04:16:55 +00:00
}
}
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))
2024-06-19 08:56:56 +00:00
}
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
}