mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 14:20:04 -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:
@@ -93,7 +93,7 @@ pub struct Harness<'a, State: 'static = ()> {
|
|||||||
#[cfg(feature = "snapshot")]
|
#[cfg(feature = "snapshot")]
|
||||||
default_snapshot_options: SnapshotOptions,
|
default_snapshot_options: SnapshotOptions,
|
||||||
#[cfg(feature = "snapshot")]
|
#[cfg(feature = "snapshot")]
|
||||||
snapshot_results: Option<SnapshotResults>,
|
snapshot_results: SnapshotResults,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State> Debug for Harness<'_, State> {
|
impl<State> Debug for Harness<'_, State> {
|
||||||
@@ -185,7 +185,7 @@ impl<'a, State: 'static> Harness<'a, State> {
|
|||||||
default_snapshot_options,
|
default_snapshot_options,
|
||||||
|
|
||||||
#[cfg(feature = "snapshot")]
|
#[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
|
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
|
||||||
harness.run_ok();
|
harness.run_ok();
|
||||||
@@ -930,12 +930,10 @@ impl<State: 'static> Drop for Harness<'_, State> {
|
|||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// Consume SnapshotResults first so its own panic-check runs under our control,
|
// Consume SnapshotResults first so its own panic-check runs under our control,
|
||||||
// and so `std::thread::panicking()` reflects snapshot failures when plugins observe
|
// 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")]
|
#[cfg(feature = "snapshot")]
|
||||||
if let Some(results) = self.snapshot_results.take() {
|
drop(std::mem::take(&mut self.snapshot_results));
|
||||||
// Drop may panic; if so, the panic propagates and plugins still see Fail.
|
|
||||||
drop(results);
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.plugins.is_empty() {
|
if self.plugins.is_empty() {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -676,10 +676,7 @@ impl<State: 'static> Harness<'_, State> {
|
|||||||
#[track_caller]
|
#[track_caller]
|
||||||
pub fn snapshot_options(&mut self, name: impl Into<String>, options: &SnapshotOptions) {
|
pub fn snapshot_options(&mut self, name: impl Into<String>, options: &SnapshotOptions) {
|
||||||
let result = self.try_snapshot_options(name, options);
|
let result = self.try_snapshot_options(name, options);
|
||||||
self.snapshot_results
|
self.snapshot_results.add(result);
|
||||||
.as_mut()
|
|
||||||
.expect("SnapshotResults already taken")
|
|
||||||
.add(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
|
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
|
||||||
@@ -696,10 +693,7 @@ impl<State: 'static> Harness<'_, State> {
|
|||||||
#[track_caller]
|
#[track_caller]
|
||||||
pub fn snapshot(&mut self, name: impl Into<String>) {
|
pub fn snapshot(&mut self, name: impl Into<String>) {
|
||||||
let result = self.try_snapshot(name);
|
let result = self.try_snapshot(name);
|
||||||
self.snapshot_results
|
self.snapshot_results.add(result);
|
||||||
.as_mut()
|
|
||||||
.expect("SnapshotResults already taken")
|
|
||||||
.add(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render a snapshot, save it to a temp file and open it in the default image viewer.
|
/// Render a snapshot, save it to a temp file and open it in the default image viewer.
|
||||||
@@ -752,10 +746,7 @@ impl<State: 'static> Harness<'_, State> {
|
|||||||
/// This removes the snapshot results from the harness. Useful if you e.g. want to merge it
|
/// 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`]).
|
/// with the results from another harness (using [`SnapshotResults::add`]).
|
||||||
pub fn take_snapshot_results(&mut self) -> SnapshotResults {
|
pub fn take_snapshot_results(&mut self) -> SnapshotResults {
|
||||||
// Replace with a fresh SnapshotResults so subsequent snapshot calls don't panic.
|
std::mem::take(&mut self.snapshot_results)
|
||||||
self.snapshot_results
|
|
||||||
.replace(SnapshotResults::default())
|
|
||||||
.expect("SnapshotResults already taken")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user