reduce size of Unit enum

This commit is contained in:
Connor Skees 2020-07-08 21:39:30 -04:00
parent 2dfda192bc
commit 65b281d1f7

View File

@ -3,6 +3,8 @@ use std::{
ops::{Div, Mul}, ops::{Div, Mul},
}; };
use crate::interner::InternedString;
pub(crate) use conversion::UNIT_CONVERSION_TABLE; pub(crate) use conversion::UNIT_CONVERSION_TABLE;
mod conversion; mod conversion;
@ -99,12 +101,13 @@ pub(crate) enum Unit {
Percent, Percent,
/// Unknown unit /// Unknown unit
Unknown(Box<str>), Unknown(InternedString),
/// Unspecified unit /// Unspecified unit
None, None,
/// Units multiplied together /// Units multiplied together
Mul(Box<[Unit]>), /// Boxed under the assumption that mul units are exceedingly rare
Mul(Box<Vec<Unit>>),
/// Units divided by each other /// Units divided by each other
Div(Box<DivUnit>), Div(Box<DivUnit>),
@ -163,11 +166,11 @@ impl Mul<Unit> for DivUnit {
Unit::Mul(..) => todo!(), Unit::Mul(..) => todo!(),
Unit::Div(..) => unreachable!(), Unit::Div(..) => unreachable!(),
Unit::None => { Unit::None => {
let numer = Unit::Mul(vec![rhs].into_boxed_slice()); let numer = Unit::Mul(Box::new(vec![rhs]));
Unit::Div(Box::new(DivUnit::new(numer, self.denom))) Unit::Div(Box::new(DivUnit::new(numer, self.denom)))
} }
_ => { _ => {
let numer = Unit::Mul(vec![self.numer, rhs].into_boxed_slice()); let numer = Unit::Mul(Box::new(vec![self.numer, rhs]));
Unit::Div(Box::new(DivUnit::new(numer, self.denom))) Unit::Div(Box::new(DivUnit::new(numer, self.denom)))
} }
}, },
@ -191,15 +194,15 @@ impl Mul<Unit> for Unit {
match self { match self {
Unit::Mul(u) => match rhs { Unit::Mul(u) => match rhs {
Unit::Mul(u2) => { Unit::Mul(u2) => {
let mut unit1 = u.into_vec(); let mut unit1 = *u;
unit1.extend_from_slice(&*u2); unit1.extend_from_slice(&*u2);
Unit::Mul(unit1.into_boxed_slice()) Unit::Mul(Box::new(unit1))
} }
Unit::Div(..) => todo!(), Unit::Div(..) => todo!(),
_ => { _ => {
let mut unit1 = u.into_vec(); let mut unit1 = *u;
unit1.push(rhs); unit1.push(rhs);
Unit::Mul(unit1.into_boxed_slice()) Unit::Mul(Box::new(unit1))
} }
}, },
Unit::Div(div) => *div * rhs, Unit::Div(div) => *div * rhs,
@ -207,10 +210,10 @@ impl Mul<Unit> for Unit {
Unit::Mul(u2) => { Unit::Mul(u2) => {
let mut unit1 = vec![self]; let mut unit1 = vec![self];
unit1.extend_from_slice(&*u2); unit1.extend_from_slice(&*u2);
Unit::Mul(unit1.into_boxed_slice()) Unit::Mul(Box::new(unit1))
} }
Unit::Div(..) => todo!(), Unit::Div(..) => todo!(),
_ => Unit::Mul(vec![self, rhs].into_boxed_slice()), _ => Unit::Mul(Box::new(vec![self, rhs])),
}, },
} }
} }
@ -308,7 +311,7 @@ impl From<String> for Unit {
"dppx" => Unit::Dppx, "dppx" => Unit::Dppx,
"x" => Unit::X, "x" => Unit::X,
"fr" => Unit::Fr, "fr" => Unit::Fr,
_ => Unit::Unknown(unit.into_boxed_str()), _ => Unit::Unknown(InternedString::get_or_intern(unit)),
} }
} }
} }