diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-07-10 20:22:52 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-07-10 23:10:35 +0200 |
| commit | 982ce85976913463eed6c95d3599868c5e1a79dd (patch) | |
| tree | f546eeb2f7c946a55dfda65ce446ec5671cb8dc9 /src/eval/value.rs | |
| parent | 6a4823461f491aef63451f097ddfe5602e0b2157 (diff) | |
Move comparisons into standard traits
Diffstat (limited to 'src/eval/value.rs')
| -rw-r--r-- | src/eval/value.rs | 47 |
1 files changed, 13 insertions, 34 deletions
diff --git a/src/eval/value.rs b/src/eval/value.rs index fe9494b1..7c35fdbd 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -10,7 +10,7 @@ use crate::geom::{Angle, Fractional, Length, Linear, Relative}; use crate::syntax::{Span, Spanned}; /// A computational value. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone)] pub enum Value { /// The value that indicates the absence of a meaningful value. None, @@ -83,39 +83,6 @@ impl Value { } } - /// Recursively compute whether two values are equal. - pub fn eq(&self, rhs: &Self) -> bool { - match (self, rhs) { - (&Self::Int(a), &Self::Float(b)) => a as f64 == b, - (&Self::Float(a), &Self::Int(b)) => a == b as f64, - (&Self::Length(a), &Self::Linear(b)) => a == b.abs && b.rel.is_zero(), - (&Self::Relative(a), &Self::Linear(b)) => a == b.rel && b.abs.is_zero(), - (&Self::Linear(a), &Self::Length(b)) => a.abs == b && a.rel.is_zero(), - (&Self::Linear(a), &Self::Relative(b)) => a.rel == b && a.abs.is_zero(), - (Self::Array(a), Self::Array(b)) => { - a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x.eq(y)) - } - (Self::Dict(a), Self::Dict(b)) => { - a.len() == b.len() - && a.iter().all(|(k, x)| b.get(k).map_or(false, |y| x.eq(y))) - } - (a, b) => a == b, - } - } - - /// Compare a value with another value. - pub fn cmp(&self, rhs: &Self) -> Option<Ordering> { - match (self, rhs) { - (Self::Int(a), Self::Int(b)) => a.partial_cmp(b), - (Self::Int(a), Self::Float(b)) => (*a as f64).partial_cmp(b), - (Self::Float(a), Self::Int(b)) => a.partial_cmp(&(*b as f64)), - (Self::Float(a), Self::Float(b)) => a.partial_cmp(b), - (Self::Angle(a), Self::Angle(b)) => a.partial_cmp(b), - (Self::Length(a), Self::Length(b)) => a.partial_cmp(b), - _ => None, - } - } - /// Try to cast the value into a specific type. pub fn cast<T>(self) -> CastResult<T, Self> where @@ -143,6 +110,18 @@ impl Default for Value { } } +impl PartialEq for Value { + fn eq(&self, other: &Self) -> bool { + ops::equal(self, other) + } +} + +impl PartialOrd for Value { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + ops::compare(self, other) + } +} + /// A wrapper around a dynamic value. pub struct AnyValue(Box<dyn Bounds>); |
