From 3d1a7a33e14d35b6c3245b8eaa22ef9032bc7c8d Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 28 Aug 2023 11:43:24 +0100 Subject: [PATCH 1/2] Removed `Val`'s `try_*` arithmetic functions --- crates/bevy_ui/src/ui_node.rs | 181 +--------------------------------- 1 file changed, 1 insertion(+), 180 deletions(-) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 435a14b1c01f8..6e7406a09b30c 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -234,42 +234,6 @@ pub enum ValArithmeticError { } impl Val { - /// Tries to add the values of two [`Val`]s. - /// Returns [`ValArithmeticError::NonIdenticalVariants`] if two [`Val`]s are of different variants. - /// When adding non-numeric [`Val`]s, it returns the value unchanged. - pub fn try_add(&self, rhs: Val) -> Result { - match (self, rhs) { - (Val::Auto, Val::Auto) => Ok(*self), - (Val::Px(value), Val::Px(rhs_value)) => Ok(Val::Px(value + rhs_value)), - (Val::Percent(value), Val::Percent(rhs_value)) => Ok(Val::Percent(value + rhs_value)), - _ => Err(ValArithmeticError::NonIdenticalVariants), - } - } - - /// Adds `rhs` to `self` and assigns the result to `self` (see [`Val::try_add`]) - pub fn try_add_assign(&mut self, rhs: Val) -> Result<(), ValArithmeticError> { - *self = self.try_add(rhs)?; - Ok(()) - } - - /// Tries to subtract the values of two [`Val`]s. - /// Returns [`ValArithmeticError::NonIdenticalVariants`] if two [`Val`]s are of different variants. - /// When adding non-numeric [`Val`]s, it returns the value unchanged. - pub fn try_sub(&self, rhs: Val) -> Result { - match (self, rhs) { - (Val::Auto, Val::Auto) => Ok(*self), - (Val::Px(value), Val::Px(rhs_value)) => Ok(Val::Px(value - rhs_value)), - (Val::Percent(value), Val::Percent(rhs_value)) => Ok(Val::Percent(value - rhs_value)), - _ => Err(ValArithmeticError::NonIdenticalVariants), - } - } - - /// Subtracts `rhs` from `self` and assigns the result to `self` (see [`Val::try_sub`]) - pub fn try_sub_assign(&mut self, rhs: Val) -> Result<(), ValArithmeticError> { - *self = self.try_sub(rhs)?; - Ok(()) - } - /// A convenience function for simple evaluation of [`Val::Percent`] variant into a concrete [`Val::Px`] value. /// Returns a [`ValArithmeticError::NonEvaluateable`] if the [`Val`] is impossible to evaluate into [`Val::Px`]. /// Otherwise it returns an [`f32`] containing the evaluated value in pixels. @@ -282,46 +246,6 @@ impl Val { _ => Err(ValArithmeticError::NonEvaluateable), } } - - /// Similar to [`Val::try_add`], but performs [`Val::evaluate`] on both values before adding. - /// Returns an [`f32`] value in pixels. - pub fn try_add_with_size(&self, rhs: Val, size: f32) -> Result { - let lhs = self.evaluate(size)?; - let rhs = rhs.evaluate(size)?; - - Ok(lhs + rhs) - } - - /// Similar to [`Val::try_add_assign`], but performs [`Val::evaluate`] on both values before adding. - /// The value gets converted to [`Val::Px`]. - pub fn try_add_assign_with_size( - &mut self, - rhs: Val, - size: f32, - ) -> Result<(), ValArithmeticError> { - *self = Val::Px(self.evaluate(size)? + rhs.evaluate(size)?); - Ok(()) - } - - /// Similar to [`Val::try_sub`], but performs [`Val::evaluate`] on both values before subtracting. - /// Returns an [`f32`] value in pixels. - pub fn try_sub_with_size(&self, rhs: Val, size: f32) -> Result { - let lhs = self.evaluate(size)?; - let rhs = rhs.evaluate(size)?; - - Ok(lhs - rhs) - } - - /// Similar to [`Val::try_sub_assign`], but performs [`Val::evaluate`] on both values before adding. - /// The value gets converted to [`Val::Px`]. - pub fn try_sub_assign_with_size( - &mut self, - rhs: Val, - size: f32, - ) -> Result<(), ValArithmeticError> { - *self = Val::Px(self.try_add_with_size(rhs, size)?); - Ok(()) - } } /// Describes the style of a UI container node @@ -1723,67 +1647,7 @@ mod tests { use super::Val; - #[test] - fn val_try_add() { - let auto_sum = Val::Auto.try_add(Val::Auto).unwrap(); - let px_sum = Val::Px(20.).try_add(Val::Px(22.)).unwrap(); - let percent_sum = Val::Percent(50.).try_add(Val::Percent(50.)).unwrap(); - - assert_eq!(auto_sum, Val::Auto); - assert_eq!(px_sum, Val::Px(42.)); - assert_eq!(percent_sum, Val::Percent(100.)); - } - - #[test] - fn val_try_add_to_self() { - let mut val = Val::Px(5.); - - val.try_add_assign(Val::Px(3.)).unwrap(); - - assert_eq!(val, Val::Px(8.)); - } - - #[test] - fn val_try_sub() { - let auto_sum = Val::Auto.try_sub(Val::Auto).unwrap(); - let px_sum = Val::Px(72.).try_sub(Val::Px(30.)).unwrap(); - let percent_sum = Val::Percent(100.).try_sub(Val::Percent(50.)).unwrap(); - - assert_eq!(auto_sum, Val::Auto); - assert_eq!(px_sum, Val::Px(42.)); - assert_eq!(percent_sum, Val::Percent(50.)); - } - - #[test] - fn different_variant_val_try_add() { - let different_variant_sum_1 = Val::Px(50.).try_add(Val::Percent(50.)); - let different_variant_sum_2 = Val::Percent(50.).try_add(Val::Auto); - - assert_eq!( - different_variant_sum_1, - Err(ValArithmeticError::NonIdenticalVariants) - ); - assert_eq!( - different_variant_sum_2, - Err(ValArithmeticError::NonIdenticalVariants) - ); - } - - #[test] - fn different_variant_val_try_sub() { - let different_variant_diff_1 = Val::Px(50.).try_sub(Val::Percent(50.)); - let different_variant_diff_2 = Val::Percent(50.).try_sub(Val::Auto); - - assert_eq!( - different_variant_diff_1, - Err(ValArithmeticError::NonIdenticalVariants) - ); - assert_eq!( - different_variant_diff_2, - Err(ValArithmeticError::NonIdenticalVariants) - ); - } - + #[test] fn val_evaluate() { let size = 250.; @@ -1808,49 +1672,6 @@ mod tests { assert_eq!(evaluate_auto, Err(ValArithmeticError::NonEvaluateable)); } - #[test] - fn val_try_add_with_size() { - let size = 250.; - - let px_sum = Val::Px(21.).try_add_with_size(Val::Px(21.), size).unwrap(); - let percent_sum = Val::Percent(20.) - .try_add_with_size(Val::Percent(30.), size) - .unwrap(); - let mixed_sum = Val::Px(20.) - .try_add_with_size(Val::Percent(30.), size) - .unwrap(); - - assert_eq!(px_sum, 42.); - assert_eq!(percent_sum, 0.5 * size); - assert_eq!(mixed_sum, 20. + 0.3 * size); - } - - #[test] - fn val_try_sub_with_size() { - let size = 250.; - - let px_sum = Val::Px(60.).try_sub_with_size(Val::Px(18.), size).unwrap(); - let percent_sum = Val::Percent(80.) - .try_sub_with_size(Val::Percent(30.), size) - .unwrap(); - let mixed_sum = Val::Percent(50.) - .try_sub_with_size(Val::Px(30.), size) - .unwrap(); - - assert_eq!(px_sum, 42.); - assert_eq!(percent_sum, 0.5 * size); - assert_eq!(mixed_sum, 0.5 * size - 30.); - } - - #[test] - fn val_try_add_non_numeric_with_size() { - let size = 250.; - - let percent_sum = Val::Auto.try_add_with_size(Val::Auto, size); - - assert_eq!(percent_sum, Err(ValArithmeticError::NonEvaluateable)); - } - #[test] fn val_arithmetic_error_messages() { assert_eq!( From 29ed42bcee558a7f8d9f6d568c97a34f77a59333 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 28 Aug 2023 11:56:14 +0100 Subject: [PATCH 2/2] cargo fmt --- crates/bevy_ui/src/ui_node.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 6e7406a09b30c..b8cb8c1d15540 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -1647,7 +1647,6 @@ mod tests { use super::Val; - #[test] fn val_evaluate() { let size = 250.;