Skip to content

Commit d69350e

Browse files
committed
Use Box to store calcs
Reduces size of Length enum (faster to move around)
1 parent 3b687d7 commit d69350e

File tree

1 file changed

+106
-60
lines changed

1 file changed

+106
-60
lines changed

src/values/length.rs

Lines changed: 106 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ use super::number::serialize_number;
1111
pub enum LengthPercentage {
1212
Length(Length),
1313
Percentage(Percentage),
14-
Calc(Calc<LengthPercentage>)
14+
Calc(Box<Calc<LengthPercentage>>)
1515
}
1616

1717
impl Parse for LengthPercentage {
1818
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> {
1919
match input.try_parse(Calc::parse) {
2020
Ok(Calc::Value(v)) => return Ok(*v),
21-
Ok(calc) => return Ok(LengthPercentage::Calc(calc)),
21+
Ok(calc) => return Ok(LengthPercentage::Calc(Box::new(calc))),
2222
_ => {}
2323
}
2424

@@ -41,7 +41,7 @@ impl std::ops::Mul<f32> for LengthPercentage {
4141
match self {
4242
LengthPercentage::Length(l) => LengthPercentage::Length(l * other),
4343
LengthPercentage::Percentage(p) => LengthPercentage::Percentage(Percentage(p.0 * other)),
44-
LengthPercentage::Calc(c) => LengthPercentage::Calc(c * other)
44+
LengthPercentage::Calc(c) => LengthPercentage::Calc(Box::new(*c * other))
4545
}
4646
}
4747
}
@@ -68,30 +68,40 @@ impl LengthPercentage {
6868
}
6969
},
7070
(LengthPercentage::Percentage(a), LengthPercentage::Percentage(b)) => Some(LengthPercentage::Percentage(Percentage(a.0 + b.0))),
71-
(LengthPercentage::Calc(Calc::Value(v)), other) => v.add_recursive(other),
72-
(other, LengthPercentage::Calc(Calc::Value(v))) => other.add_recursive(v),
73-
(LengthPercentage::Calc(Calc::Sum(a, b)), other) => {
74-
if let Some(res) = LengthPercentage::Calc(*a.clone()).add_recursive(other) {
75-
return Some(res.add(LengthPercentage::from(*b.clone())))
76-
}
77-
78-
if let Some(res) = LengthPercentage::Calc(*b.clone()).add_recursive(other) {
79-
return Some(LengthPercentage::from(*a.clone()).add(res))
71+
(LengthPercentage::Calc(a), other) => {
72+
match &**a {
73+
Calc::Value(v) => v.add_recursive(other),
74+
Calc::Sum(a, b) => {
75+
if let Some(res) = LengthPercentage::Calc(Box::new(*a.clone())).add_recursive(other) {
76+
return Some(res.add(LengthPercentage::from(*b.clone())))
77+
}
78+
79+
if let Some(res) = LengthPercentage::Calc(Box::new(*b.clone())).add_recursive(other) {
80+
return Some(LengthPercentage::from(*a.clone()).add(res))
81+
}
82+
83+
None
84+
}
85+
_ => None
8086
}
81-
82-
None
8387
}
84-
(other, LengthPercentage::Calc(Calc::Sum(a, b))) => {
85-
if let Some(res) = other.add_recursive(&LengthPercentage::Calc(*a.clone())) {
86-
return Some(res.add(LengthPercentage::from(*b.clone())))
87-
}
88-
89-
if let Some(res) = other.add_recursive(&LengthPercentage::Calc(*b.clone())) {
90-
return Some(LengthPercentage::from(*a.clone()).add(res))
88+
(other, LengthPercentage::Calc(b)) => {
89+
match &**b {
90+
Calc::Value(v) => other.add_recursive(&*v),
91+
Calc::Sum(a, b) => {
92+
if let Some(res) = other.add_recursive(&LengthPercentage::Calc(Box::new(*a.clone()))) {
93+
return Some(res.add(LengthPercentage::from(*b.clone())))
94+
}
95+
96+
if let Some(res) = other.add_recursive(&LengthPercentage::Calc(Box::new(*b.clone()))) {
97+
return Some(LengthPercentage::from(*a.clone()).add(res))
98+
}
99+
100+
None
101+
}
102+
_ => None
91103
}
92-
93-
None
94-
}
104+
},
95105
_ => None
96106
}
97107
}
@@ -113,26 +123,40 @@ impl LengthPercentage {
113123
}
114124

115125
match (a, b) {
116-
(LengthPercentage::Calc(a), LengthPercentage::Calc(b)) => LengthPercentage::Calc(a + b),
117-
(LengthPercentage::Calc(Calc::Value(a)), b) => a.add(b),
118-
(a, LengthPercentage::Calc(Calc::Value(b))) => a.add(*b),
119-
(a, b) => LengthPercentage::Calc(Calc::Sum(Box::new(a.into()), Box::new(b.into())))
126+
(LengthPercentage::Calc(a), LengthPercentage::Calc(b)) => return LengthPercentage::Calc(Box::new(*a + *b)),
127+
(LengthPercentage::Calc(calc), b) => {
128+
if let Calc::Value(a) = *calc {
129+
a.add(b)
130+
} else {
131+
LengthPercentage::Calc(Box::new(Calc::Sum(Box::new((*calc).into()), Box::new(b.into()))))
132+
}
133+
}
134+
(a, LengthPercentage::Calc(calc)) => {
135+
if let Calc::Value(b) = *calc {
136+
a.add(*b)
137+
} else {
138+
LengthPercentage::Calc(Box::new(Calc::Sum(Box::new(a.into()), Box::new((*calc).into()))))
139+
}
140+
}
141+
// (Length::Calc(Calc::Value(a)), b) => a.add(b),
142+
// (a, Length::Calc(Calc::Value(b))) => a.add(*b),
143+
(a, b) => LengthPercentage::Calc(Box::new(Calc::Sum(Box::new(a.into()), Box::new(b.into()))))
120144
}
121145
}
122146
}
123147

124148
impl std::convert::Into<Calc<LengthPercentage>> for LengthPercentage {
125149
fn into(self) -> Calc<LengthPercentage> {
126150
match self {
127-
LengthPercentage::Calc(c) => c,
151+
LengthPercentage::Calc(c) => *c,
128152
b => Calc::Value(Box::new(b))
129153
}
130154
}
131155
}
132156

133157
impl std::convert::From<Calc<LengthPercentage>> for LengthPercentage {
134158
fn from(calc: Calc<LengthPercentage>) -> LengthPercentage {
135-
LengthPercentage::Calc(calc)
159+
LengthPercentage::Calc(Box::new(calc))
136160
}
137161
}
138162

@@ -406,14 +430,14 @@ impl std::cmp::PartialOrd<f32> for RelativeLength {
406430
pub enum Length {
407431
Absolute(AbsoluteLength),
408432
Relative(RelativeLength),
409-
Calc(Calc<Length>)
433+
Calc(Box<Calc<Length>>)
410434
}
411435

412436
impl Parse for Length {
413437
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> {
414438
match input.try_parse(Calc::parse) {
415439
Ok(Calc::Value(v)) => return Ok(*v),
416-
Ok(calc) => return Ok(Length::Calc(calc)),
440+
Ok(calc) => return Ok(Length::Calc(Box::new(calc))),
417441
_ => {}
418442
}
419443

@@ -496,7 +520,7 @@ impl std::ops::Mul<f32> for Length {
496520
match self {
497521
Length::Absolute(a) => Length::Absolute(a * other),
498522
Length::Relative(a) => Length::Relative(a * other),
499-
Length::Calc(a) => Length::Calc(a * other)
523+
Length::Calc(a) => Length::Calc(Box::new(*a * other))
500524
}
501525
}
502526
}
@@ -538,30 +562,40 @@ impl Length {
538562
None
539563
}
540564
},
541-
(Length::Calc(Calc::Value(v)), other) => v.add_recursive(other),
542-
(other, Length::Calc(Calc::Value(v))) => other.add_recursive(v),
543-
(Length::Calc(Calc::Sum(a, b)), other) => {
544-
if let Some(res) = Length::Calc(*a.clone()).add_recursive(other) {
545-
return Some(res.add(Length::from(*b.clone())))
546-
}
547-
548-
if let Some(res) = Length::Calc(*b.clone()).add_recursive(other) {
549-
return Some(Length::from(*a.clone()).add(res))
565+
(Length::Calc(a), other) => {
566+
match &**a {
567+
Calc::Value(v) => v.add_recursive(other),
568+
Calc::Sum(a, b) => {
569+
if let Some(res) = Length::Calc(Box::new(*a.clone())).add_recursive(other) {
570+
return Some(res.add(Length::from(*b.clone())))
571+
}
572+
573+
if let Some(res) = Length::Calc(Box::new(*b.clone())).add_recursive(other) {
574+
return Some(Length::from(*a.clone()).add(res))
575+
}
576+
577+
None
578+
}
579+
_ => None
550580
}
551-
552-
None
553581
}
554-
(other, Length::Calc(Calc::Sum(a, b))) => {
555-
if let Some(res) = other.add_recursive(&Length::Calc(*a.clone())) {
556-
return Some(res.add(Length::from(*b.clone())))
557-
}
558-
559-
if let Some(res) = other.add_recursive(&Length::Calc(*b.clone())) {
560-
return Some(Length::from(*a.clone()).add(res))
582+
(other, Length::Calc(b)) => {
583+
match &**b {
584+
Calc::Value(v) => other.add_recursive(&*v),
585+
Calc::Sum(a, b) => {
586+
if let Some(res) = other.add_recursive(&Length::Calc(Box::new(*a.clone()))) {
587+
return Some(res.add(Length::from(*b.clone())))
588+
}
589+
590+
if let Some(res) = other.add_recursive(&Length::Calc(Box::new(*b.clone()))) {
591+
return Some(Length::from(*a.clone()).add(res))
592+
}
593+
594+
None
595+
}
596+
_ => None
561597
}
562-
563-
None
564-
}
598+
},
565599
_ => None
566600
}
567601
}
@@ -583,26 +617,38 @@ impl Length {
583617
}
584618

585619
match (a, b) {
586-
(Length::Calc(a), Length::Calc(b)) => Length::Calc(a + b),
587-
(Length::Calc(Calc::Value(a)), b) => a.add(b),
588-
(a, Length::Calc(Calc::Value(b))) => a.add(*b),
589-
(a, b) => Length::Calc(Calc::Sum(Box::new(a.into()), Box::new(b.into())))
620+
(Length::Calc(a), Length::Calc(b)) => return Length::Calc(Box::new(*a + *b)),
621+
(Length::Calc(calc), b) => {
622+
if let Calc::Value(a) = *calc {
623+
a.add(b)
624+
} else {
625+
Length::Calc(Box::new(Calc::Sum(Box::new((*calc).into()), Box::new(b.into()))))
626+
}
627+
}
628+
(a, Length::Calc(calc)) => {
629+
if let Calc::Value(b) = *calc {
630+
a.add(*b)
631+
} else {
632+
Length::Calc(Box::new(Calc::Sum(Box::new(a.into()), Box::new((*calc).into()))))
633+
}
634+
}
635+
(a, b) => Length::Calc(Box::new(Calc::Sum(Box::new(a.into()), Box::new(b.into()))))
590636
}
591637
}
592638
}
593639

594640
impl std::convert::Into<Calc<Length>> for Length {
595641
fn into(self) -> Calc<Length> {
596642
match self {
597-
Length::Calc(c) => c,
643+
Length::Calc(c) => *c,
598644
b => Calc::Value(Box::new(b))
599645
}
600646
}
601647
}
602648

603649
impl std::convert::From<Calc<Length>> for Length {
604650
fn from(calc: Calc<Length>) -> Length {
605-
Length::Calc(calc)
651+
Length::Calc(Box::new(calc))
606652
}
607653
}
608654

0 commit comments

Comments
 (0)