1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 14:49:06 -04:00

Use mem::take for snapshot_results instead of Option

The wrapping Option only existed so Drop could consume SnapshotResults
before plugins fire. mem::take swaps in a fresh default instead, dropping
the original — same outcome with no Option-juggling at every call site
(no more .as_mut().expect(...), and take_snapshot_results stops needing
.replace()).

The default SnapshotResults has handled = true so dropping the placeholder
is a no-op.
This commit is contained in:
lucasmerlin
2026-04-27 13:38:11 +02:00
parent 2381b19c81
commit 5e7c70b75b
2 changed files with 8 additions and 19 deletions

View File

@@ -93,7 +93,7 @@ pub struct Harness<'a, State: 'static = ()> {
#[cfg(feature = "snapshot")]
default_snapshot_options: SnapshotOptions,
#[cfg(feature = "snapshot")]
snapshot_results: Option<SnapshotResults>,
snapshot_results: SnapshotResults,
}
impl<State> Debug for Harness<'_, State> {
@@ -185,7 +185,7 @@ impl<'a, State: 'static> Harness<'a, State> {
default_snapshot_options,
#[cfg(feature = "snapshot")]
snapshot_results: Some(SnapshotResults::default()),
snapshot_results: SnapshotResults::default(),
};
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
harness.run_ok();
@@ -934,12 +934,10 @@ impl<State: 'static> Drop for Harness<'_, State> {
fn drop(&mut self) {
// Consume SnapshotResults first so its own panic-check runs under our control,
// and so `std::thread::panicking()` reflects snapshot failures when plugins observe
// the final outcome.
// the final outcome. Drop may panic; if so, the panic propagates and plugins still
// see Fail.
#[cfg(feature = "snapshot")]
if let Some(results) = self.snapshot_results.take() {
// Drop may panic; if so, the panic propagates and plugins still see Fail.
drop(results);
}
drop(std::mem::take(&mut self.snapshot_results));
if self.plugins.is_empty() {
return;

View File

@@ -675,10 +675,7 @@ impl<State: 'static> Harness<'_, State> {
#[track_caller]
pub fn snapshot_options(&mut self, name: impl Into<String>, options: &SnapshotOptions) {
let result = self.try_snapshot_options(name, options);
self.snapshot_results
.as_mut()
.expect("SnapshotResults already taken")
.add(result);
self.snapshot_results.add(result);
}
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
@@ -695,10 +692,7 @@ impl<State: 'static> Harness<'_, State> {
#[track_caller]
pub fn snapshot(&mut self, name: impl Into<String>) {
let result = self.try_snapshot(name);
self.snapshot_results
.as_mut()
.expect("SnapshotResults already taken")
.add(result);
self.snapshot_results.add(result);
}
/// Render a snapshot, save it to a temp file and open it in the default image viewer.
@@ -751,10 +745,7 @@ impl<State: 'static> Harness<'_, State> {
/// This removes the snapshot results from the harness. Useful if you e.g. want to merge it
/// with the results from another harness (using [`SnapshotResults::add`]).
pub fn take_snapshot_results(&mut self) -> SnapshotResults {
// Replace with a fresh SnapshotResults so subsequent snapshot calls don't panic.
self.snapshot_results
.replace(SnapshotResults::default())
.expect("SnapshotResults already taken")
std::mem::take(&mut self.snapshot_results)
}
}