Optimize size of Key and Value types
Down from 32 or 40 bytes to 16 bytes each, although with an added indirection for strings.
This commit is contained in:
parent
c24808f9f8
commit
c1bea626b1
1 changed files with 12 additions and 6 deletions
18
src/main.rs
18
src/main.rs
|
|
@ -35,7 +35,7 @@ impl Hash for Table {
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Hash)]
|
#[derive(PartialEq, Eq, Hash)]
|
||||||
enum Key {
|
enum Key {
|
||||||
String(String),
|
String(Box<String>),
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
Int(i64),
|
Int(i64),
|
||||||
Table(Table),
|
Table(Table),
|
||||||
|
|
@ -54,14 +54,20 @@ impl Debug for Key {
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum Value {
|
enum Value {
|
||||||
Key(Key),
|
String(Box<String>),
|
||||||
|
Bool(bool),
|
||||||
|
Int(i64),
|
||||||
|
Table(Table),
|
||||||
Float(f64),
|
Float(f64),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for Value {
|
impl Debug for Value {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Key(k) => k.fmt(f),
|
Self::String(s) => s.fmt(f),
|
||||||
|
Self::Bool(b) => b.fmt(f),
|
||||||
|
Self::Int(i) => i.fmt(f),
|
||||||
|
Self::Table(t) => t.fmt(f),
|
||||||
Self::Float(d) => d.fmt(f),
|
Self::Float(d) => d.fmt(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -72,11 +78,11 @@ fn main() {
|
||||||
|
|
||||||
let mut table = HashMap::new();
|
let mut table = HashMap::new();
|
||||||
table.insert(
|
table.insert(
|
||||||
Key::String("Hello".into()),
|
Key::String(Box::new("Hello".into())),
|
||||||
Value::Key(Key::String("World".into())),
|
Value::String(Box::new("World".into())),
|
||||||
);
|
);
|
||||||
let table = Rc::new(RefCell::new(table));
|
let table = Rc::new(RefCell::new(table));
|
||||||
|
|
||||||
let table_value = Value::Key(Key::Table(Table(Rc::downgrade(&table))));
|
let table_value = Value::Table(Table(Rc::downgrade(&table)));
|
||||||
dbg!(table_value);
|
dbg!(table_value);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue