Extract to a module
This commit is contained in:
parent
a6544f044e
commit
247cf593ca
48
src/lib.rs
48
src/lib.rs
|
|
@ -1,47 +1 @@
|
||||||
use std::{cell::RefCell, collections::HashMap, hash::Hash, rc::Rc};
|
pub mod types;
|
||||||
|
|
||||||
#[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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
47
src/types.rs
Normal file
47
src/types.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user