diff --git a/src/lib.rs b/src/lib.rs index 8147462..cd40856 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,47 +1 @@ -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; - -#[derive(Debug, Clone, Default)] -struct TableInner { - content: HashMap, -} - -#[derive(Debug, Clone, Default)] -pub struct Table(Rc>); - -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(&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); - } - } -} +pub mod types; diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..8147462 --- /dev/null +++ b/src/types.rs @@ -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; + +#[derive(Debug, Clone, Default)] +struct TableInner { + content: HashMap, +} + +#[derive(Debug, Clone, Default)] +pub struct Table(Rc>); + +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(&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); + } + } +}