From c24808f9f8d695ac5e109f8e2944aa5e11bbeb5a Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 26 Sep 2021 14:13:24 +0200 Subject: [PATCH] Add basic data types Including debug implementations --- src/main.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/main.rs b/src/main.rs index e7a11a9..a7f4af2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,82 @@ +use std::cell::RefCell; +use std::collections::HashMap; +use std::fmt::{self, Debug}; +use std::hash::{Hash, Hasher}; +use std::rc::{Rc, Weak}; + +struct Table(Weak>>); + +impl Debug for Table { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.0.upgrade() { + None => write!(f, ""), + Some(rc) => { + // This may panic if we're not careful? + let hash_map = &*rc.borrow(); + hash_map.fmt(f) + } + } + } +} + +impl PartialEq for Table { + fn eq(&self, other: &Self) -> bool { + self.0.ptr_eq(&other.0) + } +} + +impl Eq for Table {} + +impl Hash for Table { + fn hash(&self, state: &mut H) { + self.0.as_ptr().hash(state); + } +} + +#[derive(PartialEq, Eq, Hash)] +enum Key { + String(String), + Bool(bool), + Int(i64), + Table(Table), +} + +impl Debug for Key { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::String(s) => s.fmt(f), + Self::Bool(b) => b.fmt(f), + Self::Int(i) => i.fmt(f), + Self::Table(t) => t.fmt(f), + } + } +} + +#[derive(PartialEq)] +enum Value { + Key(Key), + Float(f64), +} + +impl Debug for Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Key(k) => k.fmt(f), + Self::Float(d) => d.fmt(f), + } + } +} + fn main() { println!("Hello, world!"); + + let mut table = HashMap::new(); + table.insert( + Key::String("Hello".into()), + Value::Key(Key::String("World".into())), + ); + let table = Rc::new(RefCell::new(table)); + + let table_value = Value::Key(Key::Table(Table(Rc::downgrade(&table)))); + dbg!(table_value); }