1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Add features extra_asserts and extra_debug_asserts for more asserts

This replaces all debug_asserts with these opt-in asserts

Related: https://github.com/emilk/egui/issues/395
This commit is contained in:
Emil Ernerfeldt
2021-05-17 22:34:29 +02:00
parent bd5a85808a
commit 6e5b52e3bc
24 changed files with 135 additions and 58 deletions

View File

@@ -163,7 +163,7 @@ pub fn remap<T>(x: T, from: RangeInclusive<T>, to: RangeInclusive<T>) -> T
where
T: Real,
{
debug_assert!(from.start() != from.end());
crate::emath_assert!(from.start() != from.end());
let t = (x - *from.start()) / (*from.end() - *from.start());
lerp(to, t)
}
@@ -181,7 +181,7 @@ where
} else if *from.end() <= x {
*to.end()
} else {
debug_assert!(from.start() != from.end());
crate::emath_assert!(from.start() != from.end());
let t = (x - *from.start()) / (*from.end() - *from.start());
// Ensure no numerical inaccuracies sneak in:
if T::one() <= t {
@@ -200,7 +200,7 @@ pub fn clamp<T>(x: T, range: RangeInclusive<T>) -> T
where
T: Copy + PartialOrd,
{
debug_assert!(range.start() <= range.end());
crate::emath_assert!(range.start() <= range.end());
if x <= *range.start() {
*range.start()
} else if *range.end() <= x {
@@ -225,8 +225,8 @@ pub fn format_with_minimum_decimals(value: f64, decimals: usize) -> String {
pub fn format_with_decimals_in_range(value: f64, decimal_range: RangeInclusive<usize>) -> String {
let min_decimals = *decimal_range.start();
let max_decimals = *decimal_range.end();
debug_assert!(min_decimals <= max_decimals);
debug_assert!(max_decimals < 100);
crate::emath_assert!(min_decimals <= max_decimals);
crate::emath_assert!(max_decimals < 100);
let max_decimals = max_decimals.min(16);
let min_decimals = min_decimals.min(max_decimals);
@@ -381,3 +381,19 @@ fn test_normalized_angle() {
almost_eq!(normalized_angle(TAU), 0.0);
almost_eq!(normalized_angle(2.7 * TAU), -0.3 * TAU);
}
// ----------------------------------------------------------------------------
/// An assert that is only active when `egui` is compiled with the `egui_assert` feature
/// or with the `debug_egui_assert` feature in debug builds.
#[macro_export]
macro_rules! emath_assert {
($($arg:tt)*) => {
if cfg!(any(
feature = "extra_asserts",
all(feature = "extra_debug_asserts", debug_assertions),
)) {
assert!($($arg)*);
}
}
}

View File

@@ -76,7 +76,7 @@ impl Rot2 {
c: self.c / l,
s: self.s / l,
};
debug_assert!(ret.is_finite());
crate::emath_assert!(ret.is_finite());
ret
}
}

View File

@@ -33,7 +33,7 @@ pub fn best_in_range_f64(min: f64, max: f64) -> f64 {
if !max.is_finite() {
return min;
}
debug_assert!(min.is_finite() && max.is_finite());
crate::emath_assert!(min.is_finite() && max.is_finite());
let min_exponent = min.log10();
let max_exponent = max.log10();
@@ -82,7 +82,7 @@ fn is_integer(f: f64) -> bool {
}
fn to_decimal_string(v: f64) -> [i32; NUM_DECIMALS] {
debug_assert!(v < 10.0, "{:?}", v);
crate::emath_assert!(v < 10.0, "{:?}", v);
let mut digits = [0; NUM_DECIMALS];
let mut v = v.abs();
for r in digits.iter_mut() {
@@ -104,7 +104,7 @@ fn from_decimal_string(s: &[i32]) -> f64 {
/// Find the simplest integer in the range [min, max]
fn simplest_digit_closed_range(min: i32, max: i32) -> i32 {
debug_assert!(1 <= min && min <= max && max <= 9);
crate::emath_assert!(1 <= min && min <= max && max <= 9);
if min <= 5 && 5 <= max {
5
} else {