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

Add assert messages and print bad argument values in asserts (#5216)

Enabled the `missing_assert_message` lint

* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Lucas Meurer <lucasmeurer96@gmail.com>
This commit is contained in:
Nicolas
2025-03-25 09:20:29 +01:00
committed by GitHub
parent 903bd81313
commit 58b2ac88c0
35 changed files with 331 additions and 108 deletions

View File

@@ -94,7 +94,13 @@ impl ColorImage {
/// }
/// ```
pub fn from_rgba_unmultiplied(size: [usize; 2], rgba: &[u8]) -> Self {
assert_eq!(size[0] * size[1] * 4, rgba.len());
assert_eq!(
size[0] * size[1] * 4,
rgba.len(),
"size: {:?}, rgba.len(): {}",
size,
rgba.len()
);
let pixels = rgba
.chunks_exact(4)
.map(|p| Color32::from_rgba_unmultiplied(p[0], p[1], p[2], p[3]))
@@ -103,7 +109,13 @@ impl ColorImage {
}
pub fn from_rgba_premultiplied(size: [usize; 2], rgba: &[u8]) -> Self {
assert_eq!(size[0] * size[1] * 4, rgba.len());
assert_eq!(
size[0] * size[1] * 4,
rgba.len(),
"size: {:?}, rgba.len(): {}",
size,
rgba.len()
);
let pixels = rgba
.chunks_exact(4)
.map(|p| Color32::from_rgba_premultiplied(p[0], p[1], p[2], p[3]))
@@ -115,7 +127,13 @@ impl ColorImage {
///
/// Panics if `size[0] * size[1] != gray.len()`.
pub fn from_gray(size: [usize; 2], gray: &[u8]) -> Self {
assert_eq!(size[0] * size[1], gray.len());
assert_eq!(
size[0] * size[1],
gray.len(),
"size: {:?}, gray.len(): {}",
size,
gray.len()
);
let pixels = gray.iter().map(|p| Color32::from_gray(*p)).collect();
Self { size, pixels }
}
@@ -127,7 +145,13 @@ impl ColorImage {
#[doc(alias = "from_grey_iter")]
pub fn from_gray_iter(size: [usize; 2], gray_iter: impl Iterator<Item = u8>) -> Self {
let pixels: Vec<_> = gray_iter.map(Color32::from_gray).collect();
assert_eq!(size[0] * size[1], pixels.len());
assert_eq!(
size[0] * size[1],
pixels.len(),
"size: {:?}, pixels.len(): {}",
size,
pixels.len()
);
Self { size, pixels }
}
@@ -150,7 +174,13 @@ impl ColorImage {
///
/// Panics if `size[0] * size[1] * 3 != rgb.len()`.
pub fn from_rgb(size: [usize; 2], rgb: &[u8]) -> Self {
assert_eq!(size[0] * size[1] * 3, rgb.len());
assert_eq!(
size[0] * size[1] * 3,
rgb.len(),
"size: {:?}, rgb.len(): {}",
size,
rgb.len()
);
let pixels = rgb
.chunks_exact(3)
.map(|p| Color32::from_rgb(p[0], p[1], p[2]))
@@ -225,7 +255,7 @@ impl std::ops::Index<(usize, usize)> for ColorImage {
#[inline]
fn index(&self, (x, y): (usize, usize)) -> &Color32 {
let [w, h] = self.size;
assert!(x < w && y < h);
assert!(x < w && y < h, "x: {x}, y: {y}, w: {w}, h: {h}");
&self.pixels[y * w + x]
}
}
@@ -234,7 +264,7 @@ impl std::ops::IndexMut<(usize, usize)> for ColorImage {
#[inline]
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Color32 {
let [w, h] = self.size;
assert!(x < w && y < h);
assert!(x < w && y < h, "x: {x}, y: {y}, w: {w}, h: {h}");
&mut self.pixels[y * w + x]
}
}
@@ -328,15 +358,32 @@ impl FontImage {
/// Clone a sub-region as a new image.
pub fn region(&self, [x, y]: [usize; 2], [w, h]: [usize; 2]) -> Self {
assert!(x + w <= self.width());
assert!(y + h <= self.height());
assert!(
x + w <= self.width(),
"x + w should be <= self.width(), but x: {}, w: {}, width: {}",
x,
w,
self.width()
);
assert!(
y + h <= self.height(),
"y + h should be <= self.height(), but y: {}, h: {}, height: {}",
y,
h,
self.height()
);
let mut pixels = Vec::with_capacity(w * h);
for y in y..y + h {
let offset = y * self.width() + x;
pixels.extend(&self.pixels[offset..(offset + w)]);
}
assert_eq!(pixels.len(), w * h);
assert_eq!(
pixels.len(),
w * h,
"pixels.len should be w * h, but got {}",
pixels.len()
);
Self {
size: [w, h],
pixels,
@@ -350,7 +397,7 @@ impl std::ops::Index<(usize, usize)> for FontImage {
#[inline]
fn index(&self, (x, y): (usize, usize)) -> &f32 {
let [w, h] = self.size;
assert!(x < w && y < h);
assert!(x < w && y < h, "x: {x}, y: {y}, w: {w}, h: {h}");
&self.pixels[y * w + x]
}
}
@@ -359,7 +406,7 @@ impl std::ops::IndexMut<(usize, usize)> for FontImage {
#[inline]
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut f32 {
let [w, h] = self.size;
assert!(x < w && y < h);
assert!(x < w && y < h, "x: {x}, y: {y}, w: {w}, h: {h}");
&mut self.pixels[y * w + x]
}
}