use dashmap::DashMap;
use std::sync::{Arc, LazyLock};
use uuid::Uuid;
static INSTANCES: LazyLock<DashMap<Uuid, &'static str>> = LazyLock::new(DashMap::new);
pub fn short_type_name(full: &str) -> &str {
full.rsplit("::").next().unwrap_or(full)
}
pub fn register_instance(id: Uuid, struct_type_name: &'static str) {
INSTANCES.insert(id, struct_type_name);
}
pub fn deregister_instance(id: Uuid) {
INSTANCES.remove(&id);
}
pub fn resolve_instance(id: Uuid) -> Option<&'static str> {
INSTANCES.get(&id).map(|found| *found)
}
pub fn resolve_instance_short(id: Uuid) -> Option<&'static str> {
resolve_instance(id).map(short_type_name)
}
pub struct InstanceGuard {
id: Uuid,
}
impl InstanceGuard {
pub fn new(id: Uuid, struct_type_name: &'static str) -> Arc<Self> {
register_instance(id, struct_type_name);
Arc::new(Self { id })
}
pub fn id(&self) -> Uuid {
self.id
}
}
impl Drop for InstanceGuard {
fn drop(&mut self) {
deregister_instance(self.id);
}
}