SeaLang/src/ast2/internable.rs
2025-09-19 20:12:21 +02:00

35 lines
1 KiB
Rust

use super::*;
use core::hash::Hash;
use std::hash::Hasher;
// Types implementing this trait can be stored in the internpool.
trait KeyTrait: Hash + Eq {
const TAG: Tag;
fn serialise(self, pool: &mut InternPool);
fn deserialise(index: Index, pool: &mut InternPool) -> Self;
}
impl KeyTrait for String {
const TAG: Tag = Tag::String;
fn serialise(self, pool: &mut InternPool) {
todo!()
}
fn deserialise(index: Index, pool: &mut InternPool) -> Self {
// let mut hasher = std::hash::DefaultHasher::new();
// core::any::TypeId::of::<Self>().hash(&mut hasher);
// let tag = hasher.finish() as u32;
let item = pool.get_item(index).unwrap();
assert_eq!(item.tag, Self::TAG);
let start = pool.words[item.idx()] as usize;
let len = pool.words[item.idx() + 1] as usize;
let str = unsafe {
let bytes = &pool.strings[start..start + len];
std::str::from_utf8_unchecked(bytes)
};
str.to_owned()
}
}