From 91ef877f2f9f5fd8a84caac612f81b0616bebc6c Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 4 Oct 2026 13:26:13 -0400 Subject: [PATCH] Never crash: algo pyramids and ui-egui parity without expect (#40) Three remaining expect() sites in files no open PR touches: algo::inpaint::complete and algo::pyramid::gaussian loop with while-let instead of levels.last().expect(), and ui-egui parity::compute indexes the menu it just pushed instead of last_mut().expect(). Co-authored-by: Claude Opus 5.5 --- crates/algo/src/inpaint.rs | 3 +-- crates/algo/src/pyramid.rs | 2 +- crates/ui-egui/src/parity.rs | 7 ++++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/algo/src/inpaint.rs b/crates/algo/src/inpaint.rs index e77b9f5cf2..dca652a603 100644 --- a/crates/algo/src/inpaint.rs +++ b/crates/algo/src/inpaint.rs @@ -358,8 +358,7 @@ pub fn complete(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool], p: &C let mut levels = vec![Level { w, h, img: img.to_vec(), hole: hole.to_vec() }]; let mut ext = (bx1 - bx0).max(by1 - by0); let mut exts = vec![ext]; - loop { - let l = levels.last().expect("non-empty"); + while let Some(l) = levels.last() { if ext <= 2 * psz || l.w / 2 < 3 * psz || l.h / 2 < 3 * psz { break; } diff --git a/crates/algo/src/pyramid.rs b/crates/algo/src/pyramid.rs index 07ce46c3ea..ae00379db4 100644 --- a/crates/algo/src/pyramid.rs +++ b/crates/algo/src/pyramid.rs @@ -90,7 +90,7 @@ pub fn expand(l: &Level, ch: usize, w: usize, h: usize) -> Vec { pub fn gaussian(w: usize, h: usize, ch: usize, px: &[f32], levels: usize) -> Vec { let mut out = vec![Level { w, h, px: px.to_vec() }]; while out.len() < levels.max(1) { - let last = out.last().expect("non-empty"); + let Some(last) = out.last() else { break }; if last.w <= 1 && last.h <= 1 { break; } diff --git a/crates/ui-egui/src/parity.rs b/crates/ui-egui/src/parity.rs index be938e2acd..b30490fcc5 100644 --- a/crates/ui-egui/src/parity.rs +++ b/crates/ui-egui/src/parity.rs @@ -62,13 +62,14 @@ pub fn compute() -> Parity { continue; } let top = path.first().copied().unwrap_or("?"); - let m = match menus.iter().position(|m| m.menu == top) { - Some(i) => &mut menus[i], + let i = match menus.iter().position(|m| m.menu == top) { + Some(i) => i, None => { menus.push(MenuParity { menu: top.to_string(), live: 0, total: 0, missing: Vec::new() }); - menus.last_mut().expect("just pushed") + menus.len() - 1 } }; + let Some(m) = menus.get_mut(i) else { continue }; m.total += 1; if is_live(id) { m.live += 1;