refactor: use OnceLock for log dedup statics in processor.rs
This commit is contained in:
@@ -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<Option<HashSet<(String, i32, i32)>>> = Mutex::new(None);
|
||||
static SEEN: OnceLock<Mutex<HashSet<(String, i32, i32)>>> = 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<Option<HashSet<WindowRectKey>>> = Mutex::new(None);
|
||||
static SEEN: OnceLock<Mutex<HashSet<WindowRectKey>>> = 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,
|
||||
|
||||
Reference in New Issue
Block a user