1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 21:00:03 -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

@@ -160,7 +160,7 @@ impl Color32 {
/// Multiply with 0.5 to make color half as opaque.
pub fn linear_multiply(self, factor: f32) -> Color32 {
debug_assert!(0.0 <= factor && factor <= 1.0);
crate::epaint_assert!(0.0 <= factor && factor <= 1.0);
// As an unfortunate side-effect of using premultiplied alpha
// we need a somewhat expensive conversion to linear space and back.
Rgba::from(self).multiply(factor).into()
@@ -214,22 +214,22 @@ impl Rgba {
}
pub fn from_luminance_alpha(l: f32, a: f32) -> Self {
debug_assert!(0.0 <= l && l <= 1.0);
debug_assert!(0.0 <= a && a <= 1.0);
crate::epaint_assert!(0.0 <= l && l <= 1.0);
crate::epaint_assert!(0.0 <= a && a <= 1.0);
Self([l * a, l * a, l * a, a])
}
/// Transparent black
#[inline(always)]
pub fn from_black_alpha(a: f32) -> Self {
debug_assert!(0.0 <= a && a <= 1.0);
crate::epaint_assert!(0.0 <= a && a <= 1.0);
Self([0.0, 0.0, 0.0, a])
}
/// Transparent white
#[inline(always)]
pub fn from_white_alpha(a: f32) -> Self {
debug_assert!(0.0 <= a && a <= 1.0);
crate::epaint_assert!(0.0 <= a && a <= 1.0);
Self([a, a, a, a])
}

View File

@@ -159,3 +159,19 @@ pub struct ClippedMesh(
/// The shape
pub Mesh,
);
// ----------------------------------------------------------------------------
/// 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! epaint_assert {
($($arg:tt)*) => {
if cfg!(any(
feature = "extra_asserts",
all(feature = "extra_debug_asserts", debug_assertions),
)) {
assert!($($arg)*);
}
}
}

View File

@@ -74,7 +74,7 @@ impl Mesh {
/// Append all the indices and vertices of `other` to `self`.
pub fn append(&mut self, other: Mesh) {
debug_assert!(other.is_valid());
crate::epaint_assert!(other.is_valid());
if self.is_empty() {
*self = other;
@@ -94,7 +94,7 @@ impl Mesh {
#[inline(always)]
pub fn colored_vertex(&mut self, pos: Pos2, color: Color32) {
debug_assert!(self.texture_id == TextureId::Egui);
crate::epaint_assert!(self.texture_id == TextureId::Egui);
self.vertices.push(Vertex {
pos,
uv: WHITE_UV,
@@ -157,7 +157,7 @@ impl Mesh {
/// Uniformly colored rectangle.
#[inline(always)]
pub fn add_colored_rect(&mut self, rect: Rect, color: Color32) {
debug_assert!(self.texture_id == TextureId::Egui);
crate::epaint_assert!(self.texture_id == TextureId::Egui);
self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color)
}
@@ -166,7 +166,7 @@ impl Mesh {
/// Splits this mesh into many smaller meshes (if needed)
/// where the smaller meshes have 16-bit indices.
pub fn split_to_u16(self) -> Vec<Mesh16> {
debug_assert!(self.is_valid());
crate::epaint_assert!(self.is_valid());
const MAX_SIZE: u32 = 1 << 16;
@@ -220,7 +220,7 @@ impl Mesh {
vertices: self.vertices[(min_vindex as usize)..=(max_vindex as usize)].to_vec(),
texture_id: self.texture_id,
};
debug_assert!(mesh.is_valid());
crate::epaint_assert!(mesh.is_valid());
output.push(mesh);
}
output

View File

@@ -147,7 +147,7 @@ impl Shape {
/// ## Operations
impl Shape {
pub fn mesh(mesh: Mesh) -> Self {
debug_assert!(mesh.is_valid());
crate::epaint_assert!(mesh.is_valid());
Self::Mesh(mesh)
}

View File

@@ -456,7 +456,7 @@ fn stroke_path(
}
fn mul_color(color: Color32, factor: f32) -> Color32 {
debug_assert!(0.0 <= factor && factor <= 1.0);
crate::epaint_assert!(0.0 <= factor && factor <= 1.0);
// As an unfortunate side-effect of using premultiplied alpha
// we need a somewhat expensive conversion to linear space and back.
color.linear_multiply(factor)
@@ -528,7 +528,7 @@ impl Tessellator {
if mesh.is_valid() {
out.append(mesh);
} else {
debug_assert!(false, "Invalid Mesh in Shape::Mesh");
crate::epaint_assert!(false, "Invalid Mesh in Shape::Mesh");
}
}
Shape::LineSegment { points, stroke } => {
@@ -553,7 +553,7 @@ impl Tessellator {
}
if fill != Color32::TRANSPARENT {
debug_assert!(
crate::epaint_assert!(
closed,
"You asked to fill a path that is not closed. That makes no sense."
);
@@ -641,7 +641,10 @@ impl Tessellator {
if color == Color32::TRANSPARENT || galley.is_empty() {
return;
}
if cfg!(debug_assertions) {
if cfg!(any(
feature = "extra_asserts",
all(feature = "extra_debug_asserts", debug_assertions),
)) {
galley.sanity_check();
}
@@ -790,7 +793,7 @@ pub fn tessellate_shapes(
}
for ClippedMesh(_, mesh) in &clipped_meshes {
debug_assert!(mesh.is_valid(), "Tessellator generated invalid Mesh");
crate::epaint_assert!(mesh.is_valid(), "Tessellator generated invalid Mesh");
}
clipped_meshes

View File

@@ -467,7 +467,7 @@ impl Font {
let mut out_rows = vec![];
for (i, (x, chr)) in full_x_offsets.iter().skip(1).zip(text.chars()).enumerate() {
debug_assert!(chr != '\n');
crate::epaint_assert!(chr != '\n');
let potential_row_width = first_row_indentation + x - row_start_x;
if potential_row_width > max_width_in_points {

View File

@@ -158,9 +158,9 @@ impl Galley {
row.sanity_check();
char_count += row.char_count_including_newline();
}
debug_assert_eq!(char_count, self.text.chars().count());
crate::epaint_assert!(char_count == self.text.chars().count());
if let Some(last_row) = self.rows.last() {
debug_assert!(
crate::epaint_assert!(
!last_row.ends_with_newline,
"If the text ends with '\\n', there would be an empty row last.\n\
Galley: {:#?}",
@@ -304,7 +304,7 @@ impl Galley {
pub fn end_rcursor(&self) -> RCursor {
if let Some(last_row) = self.rows.last() {
debug_assert!(!last_row.ends_with_newline);
crate::epaint_assert!(!last_row.ends_with_newline);
RCursor {
row: self.rows.len() - 1,
column: last_row.char_count_excluding_newline(),
@@ -361,7 +361,7 @@ impl Galley {
pcursor_it.offset += row.char_count_including_newline();
}
}
debug_assert_eq!(ccursor_it, self.end().ccursor);
crate::epaint_assert!(ccursor_it == self.end().ccursor);
Cursor {
ccursor: ccursor_it, // clamp
rcursor: self.end_rcursor(),