Basic Lua data structures

This commit is contained in:
numzero 2025-04-11 14:13:20 +03:00
parent a5448c39d6
commit a6544f044e

View File

@ -0,0 +1,47 @@
use std::{cell::RefCell, collections::HashMap, hash::Hash, rc::Rc};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ValueInner {
Number(i64),
String(String),
Table(Table),
}
pub type Value = Option<ValueInner>;
#[derive(Debug, Clone, Default)]
struct TableInner {
content: HashMap<ValueInner, ValueInner>,
}
#[derive(Debug, Clone, Default)]
pub struct Table(Rc<RefCell<TableInner>>);
impl PartialEq for Table {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.0.as_ptr(), other.0.as_ptr())
}
}
impl Eq for Table {}
impl Hash for Table {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::ptr::hash(self.0.as_ptr(), state)
}
}
impl Table {
pub fn get(&self, key: ValueInner) -> Value {
self.0.borrow().content.get(&key).cloned()
}
pub fn set(&self, key: ValueInner, value: Value) {
let mut this = self.0.borrow_mut();
if let Some(value) = value {
this.content.insert(key, value);
} else {
this.content.remove(&key);
}
}
}