Skip to content

Commit 1b553c9

Browse files
committed
Remove a bunch of unwrap() (#4285)
The fewer unwraps, the fewer panics
1 parent 063098e commit 1b553c9

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

crates/eframe/src/native/glow_integration.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
#![allow(clippy::arc_with_non_send_sync)] // glow::Context was accidentally non-Sync in glow 0.13, but that will be fixed in future releases of glow: https://github.com/grovesNL/glow/commit/c4a5f7151b9b4bbb380faa06ec27415235d1bf7e
99

10-
use std::{cell::RefCell, rc::Rc, sync::Arc, time::Instant};
10+
use std::{cell::RefCell, num::NonZeroU32, rc::Rc, sync::Arc, time::Instant};
1111

1212
use glutin::{
1313
config::GlConfig,
@@ -22,9 +22,9 @@ use winit::{
2222
};
2323

2424
use egui::{
25-
epaint::ahash::HashMap, DeferredViewportUiCallback, ImmediateViewport, NumExt as _,
26-
ViewportBuilder, ViewportClass, ViewportId, ViewportIdMap, ViewportIdPair, ViewportIdSet,
27-
ViewportInfo, ViewportOutput,
25+
epaint::ahash::HashMap, DeferredViewportUiCallback, ImmediateViewport, ViewportBuilder,
26+
ViewportClass, ViewportId, ViewportIdMap, ViewportIdPair, ViewportIdSet, ViewportInfo,
27+
ViewportOutput,
2828
};
2929
#[cfg(feature = "accesskit")]
3030
use egui_winit::accesskit_winit;
@@ -252,7 +252,7 @@ impl GlowWinitApp {
252252
#[cfg(feature = "accesskit")]
253253
{
254254
let event_loop_proxy = self.repaint_proxy.lock().clone();
255-
let viewport = glutin.viewports.get_mut(&ViewportId::ROOT).unwrap();
255+
let viewport = glutin.viewports.get_mut(&ViewportId::ROOT).unwrap(); // we always have a root
256256
if let Viewport {
257257
window: Some(window),
258258
egui_winit: Some(egui_winit),
@@ -544,13 +544,17 @@ impl GlowWinitRunning {
544544
let (raw_input, viewport_ui_cb) = {
545545
let mut glutin = self.glutin.borrow_mut();
546546
let egui_ctx = glutin.egui_ctx.clone();
547-
let viewport = glutin.viewports.get_mut(&viewport_id).unwrap();
547+
let Some(viewport) = glutin.viewports.get_mut(&viewport_id) else {
548+
return EventResult::Wait;
549+
};
548550
let Some(window) = viewport.window.as_ref() else {
549551
return EventResult::Wait;
550552
};
551553
egui_winit::update_viewport_info(&mut viewport.info, &egui_ctx, window);
552554

553-
let egui_winit = viewport.egui_winit.as_mut().unwrap();
555+
let Some(egui_winit) = viewport.egui_winit.as_mut() else {
556+
return EventResult::Wait;
557+
};
554558
let mut raw_input = egui_winit.take_egui_input(window);
555559
let viewport_ui_cb = viewport.viewport_ui_cb.clone();
556560

@@ -583,8 +587,12 @@ impl GlowWinitRunning {
583587
..
584588
} = &mut *glutin;
585589
let viewport = &viewports[&viewport_id];
586-
let window = viewport.window.as_ref().unwrap();
587-
let gl_surface = viewport.gl_surface.as_ref().unwrap();
590+
let Some(window) = viewport.window.as_ref() else {
591+
return EventResult::Wait;
592+
};
593+
let Some(gl_surface) = viewport.gl_surface.as_ref() else {
594+
return EventResult::Wait;
595+
};
588596

589597
let screen_size_in_pixels: [u32; 2] = window.inner_size().into();
590598

@@ -634,7 +642,9 @@ impl GlowWinitRunning {
634642
..
635643
} = &mut *glutin;
636644

637-
let viewport = viewports.get_mut(&viewport_id).unwrap();
645+
let Some(viewport) = viewports.get_mut(&viewport_id) else {
646+
return EventResult::Wait;
647+
};
638648
viewport.info.events.clear(); // they should have been processed
639649
let window = viewport.window.clone().unwrap();
640650
let gl_surface = viewport.gl_surface.as_ref().unwrap();
@@ -865,7 +875,7 @@ impl GlutinWindowContext {
865875
crate::HardwareAcceleration::Off => Some(false),
866876
};
867877
let swap_interval = if native_options.vsync {
868-
glutin::surface::SwapInterval::Wait(std::num::NonZeroU32::new(1).unwrap())
878+
glutin::surface::SwapInterval::Wait(NonZeroU32::MIN)
869879
} else {
870880
glutin::surface::SwapInterval::DontWait
871881
};
@@ -1089,8 +1099,8 @@ impl GlutinWindowContext {
10891099

10901100
// surface attributes
10911101
let (width_px, height_px): (u32, u32) = window.inner_size().into();
1092-
let width_px = std::num::NonZeroU32::new(width_px.at_least(1)).unwrap();
1093-
let height_px = std::num::NonZeroU32::new(height_px.at_least(1)).unwrap();
1102+
let width_px = NonZeroU32::new(width_px).unwrap_or(NonZeroU32::MIN);
1103+
let height_px = NonZeroU32::new(height_px).unwrap_or(NonZeroU32::MIN);
10941104
let surface_attributes = {
10951105
use rwh_05::HasRawWindowHandle as _; // glutin stuck on old version of raw-window-handle
10961106
glutin::surface::SurfaceAttributesBuilder::<glutin::surface::WindowSurface>::new()
@@ -1169,8 +1179,8 @@ impl GlutinWindowContext {
11691179
}
11701180

11711181
fn resize(&mut self, viewport_id: ViewportId, physical_size: winit::dpi::PhysicalSize<u32>) {
1172-
let width_px = std::num::NonZeroU32::new(physical_size.width.at_least(1)).unwrap();
1173-
let height_px = std::num::NonZeroU32::new(physical_size.height.at_least(1)).unwrap();
1182+
let width_px = NonZeroU32::new(physical_size.width).unwrap_or(NonZeroU32::MIN);
1183+
let height_px = NonZeroU32::new(physical_size.height).unwrap_or(NonZeroU32::MIN);
11741184

11751185
if let Some(viewport) = self.viewports.get(&viewport_id) {
11761186
if let Some(gl_surface) = &viewport.gl_surface {

crates/eframe/src/native/wgpu_integration.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! There is a bunch of improvements we could do,
66
//! like removing a bunch of `unwraps`.
77
8-
use std::{cell::RefCell, rc::Rc, sync::Arc, time::Instant};
8+
use std::{cell::RefCell, num::NonZeroU32, rc::Rc, sync::Arc, time::Instant};
99

1010
use parking_lot::Mutex;
1111
use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};
@@ -435,13 +435,12 @@ impl WinitApp for WgpuWinitApp {
435435
self.init_run_state(egui_ctx, event_loop, storage, window, builder)?
436436
};
437437

438-
EventResult::RepaintNow(
439-
running.shared.borrow().viewports[&ViewportId::ROOT]
440-
.window
441-
.as_ref()
442-
.unwrap()
443-
.id(),
444-
)
438+
let viewport = &running.shared.borrow().viewports[&ViewportId::ROOT];
439+
if let Some(window) = &viewport.window {
440+
EventResult::RepaintNow(window.id())
441+
} else {
442+
EventResult::Wait
443+
}
445444
}
446445

447446
winit::event::Event::Suspended => {
@@ -613,7 +612,9 @@ impl WgpuWinitRunning {
613612
}
614613
}
615614

616-
let egui_winit = egui_winit.as_mut().unwrap();
615+
let Some(egui_winit) = egui_winit.as_mut() else {
616+
return EventResult::Wait;
617+
};
617618
let mut raw_input = egui_winit.take_egui_input(window);
618619

619620
integration.pre_update();
@@ -773,7 +774,6 @@ impl WgpuWinitRunning {
773774
// See: https://github.com/rust-windowing/winit/issues/208
774775
// This solves an issue where the app would panic when minimizing on Windows.
775776
if let Some(viewport_id) = viewport_id {
776-
use std::num::NonZeroU32;
777777
if let (Some(width), Some(height)) = (
778778
NonZeroU32::new(physical_size.width),
779779
NonZeroU32::new(physical_size.height),

crates/egui_glow/examples/pure_glow.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#![allow(unsafe_code)]
55
#![allow(clippy::arc_with_non_send_sync)] // glow::Context was accidentally non-Sync in glow 0.13, but that will be fixed in future releases of glow: https://github.com/grovesNL/glow/commit/c4a5f7151b9b4bbb380faa06ec27415235d1bf7e
66

7+
use std::num::NonZeroU32;
8+
79
use egui_winit::winit;
810

911
/// The majority of `GlutinWindowContext` is taken from `eframe`
@@ -19,7 +21,6 @@ impl GlutinWindowContext {
1921
// preferably add android support at the same time.
2022
#[allow(unsafe_code)]
2123
unsafe fn new(event_loop: &winit::event_loop::EventLoopWindowTarget<UserEvent>) -> Self {
22-
use egui::NumExt;
2324
use glutin::context::NotCurrentGlContext;
2425
use glutin::display::GetGlDisplay;
2526
use glutin::display::GlDisplay;
@@ -87,8 +88,8 @@ impl GlutinWindowContext {
8788
.expect("failed to finalize glutin window")
8889
});
8990
let (width, height): (u32, u32) = window.inner_size().into();
90-
let width = std::num::NonZeroU32::new(width.at_least(1)).unwrap();
91-
let height = std::num::NonZeroU32::new(height.at_least(1)).unwrap();
91+
let width = NonZeroU32::new(width).unwrap_or(NonZeroU32::MIN);
92+
let height = NonZeroU32::new(height).unwrap_or(NonZeroU32::MIN);
9293
let surface_attributes =
9394
glutin::surface::SurfaceAttributesBuilder::<glutin::surface::WindowSurface>::new()
9495
.build(window.raw_window_handle(), width, height);
@@ -107,7 +108,7 @@ impl GlutinWindowContext {
107108
gl_surface
108109
.set_swap_interval(
109110
&gl_context,
110-
glutin::surface::SwapInterval::Wait(std::num::NonZeroU32::new(1).unwrap()),
111+
glutin::surface::SwapInterval::Wait(NonZeroU32::MIN),
111112
)
112113
.unwrap();
113114

0 commit comments

Comments
 (0)