Introduce skip_next_timing in iced_debug

This is useful to avoid infinite recursion when
implementing a `sentinel` client that can inspect
itself.
This commit is contained in:
Héctor Ramón Jiménez 2024-03-01 22:04:46 +01:00
parent 1fda2d151d
commit 6097382520
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -46,6 +46,10 @@ pub fn time(window: window::Id, name: impl AsRef<str>) -> Timer {
internal::time(window, name) internal::time(window, name)
} }
pub fn skip_next_timing() {
internal::skip_next_timing();
}
#[cfg(feature = "enable")] #[cfg(feature = "enable")]
mod internal { mod internal {
use crate::core::time::{Instant, SystemTime}; use crate::core::time::{Instant, SystemTime};
@ -100,6 +104,10 @@ mod internal {
timer(timing::Stage::Custom(window, name.as_ref().to_owned())) timer(timing::Stage::Custom(window, name.as_ref().to_owned()))
} }
pub fn skip_next_timing() {
lock().skip_next_timing = true;
}
fn timer(stage: timing::Stage) -> Timer { fn timer(stage: timing::Stage) -> Timer {
Timer { Timer {
stage, stage,
@ -117,7 +125,14 @@ mod internal {
impl Timer { impl Timer {
pub fn finish(self) { pub fn finish(self) {
lock().sentinel.report_timing(Timing { let mut debug = lock();
if debug.skip_next_timing {
debug.skip_next_timing = false;
return;
}
debug.sentinel.report_timing(Timing {
stage: self.stage, stage: self.stage,
start: self.start_system_time, start: self.start_system_time,
duration: self.start.elapsed(), duration: self.start.elapsed(),
@ -129,6 +144,7 @@ mod internal {
struct Debug { struct Debug {
sentinel: Client, sentinel: Client,
last_palette: Option<theme::Palette>, last_palette: Option<theme::Palette>,
skip_next_timing: bool,
} }
fn lock() -> MutexGuard<'static, Debug> { fn lock() -> MutexGuard<'static, Debug> {
@ -136,6 +152,7 @@ mod internal {
Mutex::new(Debug { Mutex::new(Debug {
sentinel: client::connect(), sentinel: client::connect(),
last_palette: None, last_palette: None,
skip_next_timing: false,
}) })
}); });
@ -182,6 +199,8 @@ mod internal {
Timer Timer
} }
pub fn skip_next_timing() {}
#[derive(Debug)] #[derive(Debug)]
pub struct Timer; pub struct Timer;