From 8d1ebf02e3c41e9f249245ba9348f6068b59e0d7 Mon Sep 17 00:00:00 2001 From: XiuChengWu <732857315@qq.com> Date: Tue, 31 Mar 2026 23:41:10 +0800 Subject: [PATCH] refactor: use OnceLock for log dedup statics in processor.rs --- src/plugins/video/processor.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/plugins/video/processor.rs b/src/plugins/video/processor.rs index 345ceaf..2c5c7a3 100644 --- a/src/plugins/video/processor.rs +++ b/src/plugins/video/processor.rs @@ -13,28 +13,26 @@ use opencv::{ }; use rand::Rng; use std::collections::HashSet; -use std::sync::{Arc, Mutex, MutexGuard}; +use std::sync::{Arc, Mutex, MutexGuard, OnceLock}; use std::time::Instant; type WindowRectKey = (String, i32, i32, i32, i32); type LoggedSizes = ((i32, i32), (i32, i32), (i32, i32)); fn log_mat_size(label: &str, frame: &Mat) { - use std::sync::Mutex; - - static SEEN: Mutex>> = Mutex::new(None); + static SEEN: OnceLock>> = OnceLock::new(); let key = (label.to_string(), frame.cols(), frame.rows()); - let mut guard = SEEN.lock().unwrap(); - let set = guard.get_or_insert_with(HashSet::new); - if set.insert(key) { + let mut guard = SEEN + .get_or_init(|| Mutex::new(HashSet::new())) + .lock() + .unwrap(); + if guard.insert(key) { println!("{}: cols={} rows={}", label, frame.cols(), frame.rows()); } } fn log_window_image_rect(window_name: &str) { - use std::sync::Mutex; - - static SEEN: Mutex>> = Mutex::new(None); + static SEEN: OnceLock>> = OnceLock::new(); match highgui::get_window_image_rect(window_name) { Ok(rect) => { let key = ( @@ -44,9 +42,11 @@ fn log_window_image_rect(window_name: &str) { rect.width, rect.height, ); - let mut guard = SEEN.lock().unwrap(); - let set = guard.get_or_insert_with(HashSet::new); - if set.insert(key) { + let mut guard = SEEN + .get_or_init(|| Mutex::new(HashSet::new())) + .lock() + .unwrap(); + if guard.insert(key) { println!( "window.image_rect {}: x={} y={} width={} height={}", window_name, rect.x, rect.y, rect.width, rect.height,