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)]
|
||||
enum Key {
|
||||
String(String),
|
||||
String(Box<String>),
|
||||
Bool(bool),
|
||||
Int(i64),
|
||||
Table(Table),
|
||||
|
|
@ -54,14 +54,20 @@ impl Debug for Key {
|
|||
|
||||
#[derive(PartialEq)]
|
||||
enum Value {
|
||||
Key(Key),
|
||||
String(Box<String>),
|
||||
Bool(bool),
|
||||
Int(i64),
|
||||
Table(Table),
|
||||
Float(f64),
|
||||
}
|
||||
|
||||
impl Debug for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
|
@ -72,11 +78,11 @@ fn main() {
|
|||
|
||||
let mut table = HashMap::new();
|
||||
table.insert(
|
||||
Key::String("Hello".into()),
|
||||
Value::Key(Key::String("World".into())),
|
||||
Key::String(Box::new("Hello".into())),
|
||||
Value::String(Box::new("World".into())),
|
||||
);
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue