diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index 40957df9d3b..02fe6b2ba0e 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -811,7 +811,7 @@ impl Animate for Visibility { impl ComputeSquaredDistance for Visibility { #[inline] fn compute_squared_distance(&self, other: &Self) -> Result { - Ok(SquaredDistance::Value(if *self == *other { 0. } else { 1. })) + Ok(SquaredDistance::from_sqrt(if *self == *other { 0. } else { 1. })) } } @@ -1921,7 +1921,7 @@ impl ComputeSquaredDistance for Quaternion { // so we can get their angle difference by: // cos(theta/2) = (q1 dot q2) / (|q1| * |q2|) = q1 dot q2. let distance = self.dot(other).max(-1.0).min(1.0).acos() * 2.0; - Ok(SquaredDistance::Value(distance * distance)) + Ok(SquaredDistance::from_sqrt(distance)) } } diff --git a/components/style/values/animated/color.rs b/components/style/values/animated/color.rs index 4674cca8bb9..296f7bfe499 100644 --- a/components/style/values/animated/color.rs +++ b/components/style/values/animated/color.rs @@ -166,19 +166,19 @@ impl ComputeSquaredDistance for Color { // All comments from the Animate impl also applies here. if self.foreground_ratio == other.foreground_ratio { if self.is_currentcolor() { - Ok(SquaredDistance::Value(0.)) + Ok(SquaredDistance::from_sqrt(0.)) } else { self.color.compute_squared_distance(&other.color) } } else if self.is_currentcolor() && other.is_numeric() { Ok( RGBA::transparent().compute_squared_distance(&other.color)? + - SquaredDistance::Value(1.), + SquaredDistance::from_sqrt(1.), ) } else if self.is_numeric() && other.is_currentcolor() { Ok( self.color.compute_squared_distance(&RGBA::transparent())? + - SquaredDistance::Value(1.), + SquaredDistance::from_sqrt(1.), ) } else { let self_color = self.effective_intermediate_rgba(); diff --git a/components/style/values/distance.rs b/components/style/values/distance.rs index 351f1eb021f..f17b9931dda 100644 --- a/components/style/values/distance.rs +++ b/components/style/values/distance.rs @@ -28,38 +28,43 @@ pub trait ComputeSquaredDistance { /// A distance between two animatable values. #[derive(Clone, Copy, Debug)] -pub enum SquaredDistance { - /// Represented as the square root of the squared distance. - Sqrt(f64), - /// Represented as the squared distance itself. - Value(f64), +pub struct SquaredDistance { + value: f64, +} + +impl SquaredDistance { + /// Returns a squared distance from its square root. + #[inline] + pub fn from_sqrt(sqrt: f64) -> Self { + Self { value: sqrt * sqrt } + } } impl ComputeSquaredDistance for u16 { #[inline] fn compute_squared_distance(&self, other: &Self) -> Result { - Ok(SquaredDistance::Sqrt(((*self as f64) - (*other as f64)).abs())) + Ok(SquaredDistance::from_sqrt(((*self as f64) - (*other as f64)).abs())) } } impl ComputeSquaredDistance for i32 { #[inline] fn compute_squared_distance(&self, other: &Self) -> Result { - Ok(SquaredDistance::Sqrt((*self - *other).abs() as f64)) + Ok(SquaredDistance::from_sqrt((*self - *other).abs() as f64)) } } impl ComputeSquaredDistance for f32 { #[inline] fn compute_squared_distance(&self, other: &Self) -> Result { - Ok(SquaredDistance::Sqrt((*self - *other).abs() as f64)) + Ok(SquaredDistance::from_sqrt((*self - *other).abs() as f64)) } } impl ComputeSquaredDistance for f64 { #[inline] fn compute_squared_distance(&self, other: &Self) -> Result { - Ok(SquaredDistance::Sqrt((*self - *other).abs())) + Ok(SquaredDistance::from_sqrt((*self - *other).abs())) } } @@ -77,7 +82,7 @@ impl ComputeSquaredDistance for Option fn compute_squared_distance(&self, other: &Self) -> Result { match (self.as_ref(), other.as_ref()) { (Some(this), Some(other)) => this.compute_squared_distance(other), - (None, None) => Ok(SquaredDistance::Value(0.)), + (None, None) => Ok(SquaredDistance::from_sqrt(0.)), _ => Err(()), } } @@ -94,21 +99,16 @@ impl ComputeSquaredDistance for Size2D impl SquaredDistance { /// Returns the square root of this squared distance. + #[inline] pub fn sqrt(self) -> f64 { - match self { - SquaredDistance::Sqrt(this) => this, - SquaredDistance::Value(this) => this.sqrt(), - } + self.value.sqrt() } } impl From for f64 { #[inline] fn from(distance: SquaredDistance) -> Self { - match distance { - SquaredDistance::Sqrt(this) => this * this, - SquaredDistance::Value(this) => this, - } + distance.value } } @@ -117,19 +117,15 @@ impl Add for SquaredDistance { #[inline] fn add(self, rhs: Self) -> Self { - SquaredDistance::Value(f64::from(self) + f64::from(rhs)) + SquaredDistance { value: self.value + rhs.value } } } impl Sum for SquaredDistance { - fn sum(mut iter: I) -> Self + fn sum(iter: I) -> Self where I: Iterator, { - let first = match iter.next() { - Some(first) => first, - None => return SquaredDistance::Value(0.), - }; - iter.fold(first, Add::add) + iter.fold(SquaredDistance::from_sqrt(0.), Add::add) } } diff --git a/components/style_derive/compute_squared_distance.rs b/components/style_derive/compute_squared_distance.rs index 509d1b5be95..6254340e358 100644 --- a/components/style_derive/compute_squared_distance.rs +++ b/components/style_derive/compute_squared_distance.rs @@ -28,7 +28,7 @@ pub fn derive(input: DeriveInput) -> Tokens { let (this_pattern, this_info) = cg::ref_pattern(&variant, "this"); let (other_pattern, other_info) = cg::ref_pattern(&variant, "other"); let sum = if this_info.is_empty() { - quote! { ::values::distance::SquaredDistance::Value(0.) } + quote! { ::values::distance::SquaredDistance::from_sqrt(0.) } } else { let mut sum = quote!(); sum.append_separated(this_info.iter().zip(&other_info).map(|(this, other)| {