Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Val's try_* arithmetic methods #9609

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 0 additions & 180 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Val, ValArithmeticError> {
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<Val, ValArithmeticError> {
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.
Expand All @@ -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<f32, ValArithmeticError> {
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<f32, ValArithmeticError> {
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
Expand Down Expand Up @@ -1723,67 +1647,6 @@ 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.;
Expand All @@ -1808,49 +1671,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!(
Expand Down