Skip to content

Commit 941bfbf

Browse files
emilkhacknus
authored andcommitted
Remove extra_asserts and extra_debug_asserts feature flags (emilk#4478)
Removes `egui_assert` etc and replaces it with normal `debug_assert` calls. Previously you could opt-in to more runtime checks using feature flags. Now these extra runtime checks are always enabled for debug builds. You are most likely to encounter them if you use negative sizes or NaNs or other similar bugs. These usually indicate bugs in user space.
1 parent f1cd846 commit 941bfbf

32 files changed

+90
-182
lines changed

crates/ecolor/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ all-features = true
2828
[features]
2929
default = []
3030

31-
## Enable additional checks if debug assertions are enabled (debug builds).
32-
extra_debug_asserts = []
33-
## Always enable additional checks.
34-
extra_asserts = []
3531

3632
[dependencies]
3733
#! ### Optional dependencies

crates/ecolor/src/color32.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl Color32 {
199199
/// This is perceptually even, and faster that [`Self::linear_multiply`].
200200
#[inline]
201201
pub fn gamma_multiply(self, factor: f32) -> Self {
202-
crate::ecolor_assert!(0.0 <= factor && factor <= 1.0);
202+
debug_assert!(0.0 <= factor && factor <= 1.0);
203203
let Self([r, g, b, a]) = self;
204204
Self([
205205
(r as f32 * factor + 0.5) as u8,
@@ -215,7 +215,7 @@ impl Color32 {
215215
/// You likely want to use [`Self::gamma_multiply`] instead.
216216
#[inline]
217217
pub fn linear_multiply(self, factor: f32) -> Self {
218-
crate::ecolor_assert!(0.0 <= factor && factor <= 1.0);
218+
debug_assert!(0.0 <= factor && factor <= 1.0);
219219
// As an unfortunate side-effect of using premultiplied alpha
220220
// we need a somewhat expensive conversion to linear space and back.
221221
Rgba::from(self).multiply(factor).into()

crates/ecolor/src/lib.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,6 @@ pub fn gamma_from_linear(linear: f32) -> f32 {
135135

136136
// ----------------------------------------------------------------------------
137137

138-
/// An assert that is only active when `epaint` is compiled with the `extra_asserts` feature
139-
/// or with the `extra_debug_asserts` feature in debug builds.
140-
#[macro_export]
141-
macro_rules! ecolor_assert {
142-
($($arg: tt)*) => {
143-
if cfg!(any(
144-
feature = "extra_asserts",
145-
all(feature = "extra_debug_asserts", debug_assertions),
146-
)) {
147-
assert!($($arg)*);
148-
}
149-
}
150-
}
151-
152-
// ----------------------------------------------------------------------------
153-
154138
/// Cheap and ugly.
155139
/// Made for graying out disabled `Ui`s.
156140
pub fn tint_color_towards(color: Color32, target: Color32) -> Color32 {

crates/ecolor/src/rgba.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,22 @@ impl Rgba {
9898

9999
#[inline]
100100
pub fn from_luminance_alpha(l: f32, a: f32) -> Self {
101-
crate::ecolor_assert!(0.0 <= l && l <= 1.0);
102-
crate::ecolor_assert!(0.0 <= a && a <= 1.0);
101+
debug_assert!(0.0 <= l && l <= 1.0);
102+
debug_assert!(0.0 <= a && a <= 1.0);
103103
Self([l * a, l * a, l * a, a])
104104
}
105105

106106
/// Transparent black
107107
#[inline]
108108
pub fn from_black_alpha(a: f32) -> Self {
109-
crate::ecolor_assert!(0.0 <= a && a <= 1.0);
109+
debug_assert!(0.0 <= a && a <= 1.0);
110110
Self([0.0, 0.0, 0.0, a])
111111
}
112112

113113
/// Transparent white
114114
#[inline]
115115
pub fn from_white_alpha(a: f32) -> Self {
116-
crate::ecolor_assert!(0.0 <= a && a <= 1.0, "a: {}", a);
116+
debug_assert!(0.0 <= a && a <= 1.0, "a: {a}");
117117
Self([a, a, a, a])
118118
}
119119

crates/egui/Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ deadlock_detection = ["epaint/deadlock_detection"]
5252
## If you plan on specifying your own fonts you may disable this feature.
5353
default_fonts = ["epaint/default_fonts"]
5454

55-
## Enable additional checks if debug assertions are enabled (debug builds).
56-
extra_debug_asserts = ["epaint/extra_debug_asserts"]
57-
## Always enable additional checks.
58-
extra_asserts = ["epaint/extra_asserts"]
59-
6055
## Turn on the `log` feature, that makes egui log some errors using the [`log`](https://docs.rs/log) crate.
6156
log = ["dep:log", "epaint/log"]
6257

crates/egui/src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ impl Context {
17481748
let name = name.into();
17491749
let image = image.into();
17501750
let max_texture_side = self.input(|i| i.max_texture_side);
1751-
crate::egui_assert!(
1751+
debug_assert!(
17521752
image.width() <= max_texture_side && image.height() <= max_texture_side,
17531753
"Texture {:?} has size {}x{}, but the maximum texture side is {}",
17541754
name,

crates/egui/src/frame_state.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl FrameState {
117117
/// This is the "background" area, what egui doesn't cover with panels (but may cover with windows).
118118
/// This is also the area to which windows are constrained.
119119
pub(crate) fn available_rect(&self) -> Rect {
120-
crate::egui_assert!(
120+
debug_assert!(
121121
self.available_rect.is_finite(),
122122
"Called `available_rect()` before `Context::run()`"
123123
);
@@ -126,7 +126,7 @@ impl FrameState {
126126

127127
/// Shrink `available_rect`.
128128
pub(crate) fn allocate_left_panel(&mut self, panel_rect: Rect) {
129-
crate::egui_assert!(
129+
debug_assert!(
130130
panel_rect.min.distance(self.available_rect.min) < 0.1,
131131
"Mismatching left panel. You must not create a panel from within another panel."
132132
);
@@ -137,7 +137,7 @@ impl FrameState {
137137

138138
/// Shrink `available_rect`.
139139
pub(crate) fn allocate_right_panel(&mut self, panel_rect: Rect) {
140-
crate::egui_assert!(
140+
debug_assert!(
141141
panel_rect.max.distance(self.available_rect.max) < 0.1,
142142
"Mismatching right panel. You must not create a panel from within another panel."
143143
);
@@ -148,7 +148,7 @@ impl FrameState {
148148

149149
/// Shrink `available_rect`.
150150
pub(crate) fn allocate_top_panel(&mut self, panel_rect: Rect) {
151-
crate::egui_assert!(
151+
debug_assert!(
152152
panel_rect.min.distance(self.available_rect.min) < 0.1,
153153
"Mismatching top panel. You must not create a panel from within another panel."
154154
);
@@ -159,7 +159,7 @@ impl FrameState {
159159

160160
/// Shrink `available_rect`.
161161
pub(crate) fn allocate_bottom_panel(&mut self, panel_rect: Rect) {
162-
crate::egui_assert!(
162+
debug_assert!(
163163
panel_rect.max.distance(self.available_rect.max) < 0.1,
164164
"Mismatching bottom panel. You must not create a panel from within another panel."
165165
);

crates/egui/src/grid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl GridLayout {
8585
// TODO(emilk): respect current layout
8686

8787
let initial_available = ui.placer().max_rect().intersect(ui.cursor());
88-
crate::egui_assert!(
88+
debug_assert!(
8989
initial_available.min.x.is_finite(),
9090
"Grid not yet available for right-to-left layouts"
9191
);

crates/egui/src/layout.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{egui_assert, emath::*, Align};
1+
use crate::{emath::*, Align};
22
use std::f32::INFINITY;
33

44
// ----------------------------------------------------------------------------
@@ -66,9 +66,9 @@ impl Region {
6666
}
6767

6868
pub fn sanity_check(&self) {
69-
egui_assert!(!self.min_rect.any_nan());
70-
egui_assert!(!self.max_rect.any_nan());
71-
egui_assert!(!self.cursor.any_nan());
69+
debug_assert!(!self.min_rect.any_nan());
70+
debug_assert!(!self.max_rect.any_nan());
71+
debug_assert!(!self.cursor.any_nan());
7272
}
7373
}
7474

@@ -389,8 +389,8 @@ impl Layout {
389389
/// ## Doing layout
390390
impl Layout {
391391
pub fn align_size_within_rect(&self, size: Vec2, outer: Rect) -> Rect {
392-
egui_assert!(size.x >= 0.0 && size.y >= 0.0);
393-
egui_assert!(!outer.is_negative());
392+
debug_assert!(size.x >= 0.0 && size.y >= 0.0);
393+
debug_assert!(!outer.is_negative());
394394
self.align2().align_size_within_rect(size, outer)
395395
}
396396

@@ -416,8 +416,8 @@ impl Layout {
416416
}
417417

418418
pub(crate) fn region_from_max_rect(&self, max_rect: Rect) -> Region {
419-
egui_assert!(!max_rect.any_nan());
420-
egui_assert!(max_rect.is_finite());
419+
debug_assert!(!max_rect.any_nan());
420+
debug_assert!(max_rect.is_finite());
421421
let mut region = Region {
422422
min_rect: Rect::NOTHING, // temporary
423423
max_rect,
@@ -450,9 +450,9 @@ impl Layout {
450450
/// Given the cursor in the region, how much space is available
451451
/// for the next widget?
452452
fn available_from_cursor_max_rect(&self, cursor: Rect, max_rect: Rect) -> Rect {
453-
egui_assert!(!cursor.any_nan());
454-
egui_assert!(!max_rect.any_nan());
455-
egui_assert!(max_rect.is_finite());
453+
debug_assert!(!cursor.any_nan());
454+
debug_assert!(!max_rect.any_nan());
455+
debug_assert!(max_rect.is_finite());
456456

457457
// NOTE: in normal top-down layout the cursor has moved below the current max_rect,
458458
// but the available shouldn't be negative.
@@ -506,7 +506,7 @@ impl Layout {
506506
avail.max.y = y;
507507
}
508508

509-
egui_assert!(!avail.any_nan());
509+
debug_assert!(!avail.any_nan());
510510

511511
avail
512512
}
@@ -517,7 +517,7 @@ impl Layout {
517517
/// Use `justify_and_align` to get the inner `widget_rect`.
518518
pub(crate) fn next_frame(&self, region: &Region, child_size: Vec2, spacing: Vec2) -> Rect {
519519
region.sanity_check();
520-
egui_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
520+
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
521521

522522
if self.main_wrap {
523523
let available_size = self.available_rect_before_wrap(region).size();
@@ -597,7 +597,7 @@ impl Layout {
597597

598598
fn next_frame_ignore_wrap(&self, region: &Region, child_size: Vec2) -> Rect {
599599
region.sanity_check();
600-
egui_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
600+
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
601601

602602
let available_rect = self.available_rect_before_wrap(region);
603603

@@ -630,16 +630,16 @@ impl Layout {
630630
frame_rect = frame_rect.translate(Vec2::Y * (region.cursor.top() - frame_rect.top()));
631631
}
632632

633-
egui_assert!(!frame_rect.any_nan());
634-
egui_assert!(!frame_rect.is_negative());
633+
debug_assert!(!frame_rect.any_nan());
634+
debug_assert!(!frame_rect.is_negative());
635635

636636
frame_rect
637637
}
638638

639639
/// Apply justify (fill width/height) and/or alignment after calling `next_space`.
640640
pub(crate) fn justify_and_align(&self, frame: Rect, mut child_size: Vec2) -> Rect {
641-
egui_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
642-
egui_assert!(!frame.is_negative());
641+
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
642+
debug_assert!(!frame.is_negative());
643643

644644
if self.horizontal_justify() {
645645
child_size.x = child_size.x.at_least(frame.width()); // fill full width
@@ -657,10 +657,10 @@ impl Layout {
657657
) -> Rect {
658658
let frame = self.next_frame_ignore_wrap(region, size);
659659
let rect = self.align_size_within_rect(size, frame);
660-
egui_assert!(!rect.any_nan());
661-
egui_assert!(!rect.is_negative());
662-
egui_assert!((rect.width() - size.x).abs() < 1.0 || size.x == f32::INFINITY);
663-
egui_assert!((rect.height() - size.y).abs() < 1.0 || size.y == f32::INFINITY);
660+
debug_assert!(!rect.any_nan());
661+
debug_assert!(!rect.is_negative());
662+
debug_assert!((rect.width() - size.x).abs() < 1.0 || size.x == f32::INFINITY);
663+
debug_assert!((rect.height() - size.y).abs() < 1.0 || size.y == f32::INFINITY);
664664
rect
665665
}
666666

@@ -703,7 +703,7 @@ impl Layout {
703703
widget_rect: Rect,
704704
item_spacing: Vec2,
705705
) {
706-
egui_assert!(!cursor.any_nan());
706+
debug_assert!(!cursor.any_nan());
707707
if self.main_wrap {
708708
if cursor.intersects(frame_rect.shrink(1.0)) {
709709
// make row/column larger if necessary

crates/egui/src/lib.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -549,22 +549,6 @@ macro_rules! github_link_file {
549549

550550
// ----------------------------------------------------------------------------
551551

552-
/// An assert that is only active when `egui` is compiled with the `extra_asserts` feature
553-
/// or with the `extra_debug_asserts` feature in debug builds.
554-
#[macro_export]
555-
macro_rules! egui_assert {
556-
($($arg: tt)*) => {
557-
if cfg!(any(
558-
feature = "extra_asserts",
559-
all(feature = "extra_debug_asserts", debug_assertions),
560-
)) {
561-
assert!($($arg)*);
562-
}
563-
}
564-
}
565-
566-
// ----------------------------------------------------------------------------
567-
568552
/// The minus character: <https://www.compart.com/en/unicode/U+2212>
569553
pub(crate) const MINUS_CHAR_STR: &str = "−";
570554

0 commit comments

Comments
 (0)