Skip to content

Commit 99aae0e

Browse files
committed
- changed vec to hashset
- changed "if let some" to map_or_else
1 parent b98ad21 commit 99aae0e

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/backend/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use plotters_backend::{
2626
FontStyle,
2727
text_anchor,
2828
};
29+
use std::collections::HashSet;
2930
use std::sync::{LazyLock, Mutex};
3031

3132
/// The Iced drawing backend
@@ -303,26 +304,25 @@ where
303304

304305
fn style_to_font<S: BackendTextStyle>(style: &S) -> Font {
305306
// iced font family requires static str
306-
static FONTS: LazyLock<Mutex<Vec<&'static str>>> = LazyLock::new(|| Mutex::new(Vec::new()));
307+
static FONTS: LazyLock<Mutex<HashSet<&'static str>>> =
308+
LazyLock::new(|| Mutex::new(HashSet::new()));
307309

308310
Font {
309311
family: match style.family() {
310312
FontFamily::Serif => font::Family::Serif,
311313
FontFamily::SansSerif => font::Family::SansSerif,
312314
FontFamily::Monospace => font::Family::Monospace,
313-
FontFamily::Name(s) => {
314-
if let Ok(mut vec_guard) = FONTS.lock() {
315-
if let Some(&s) = vec_guard.iter().find(|&&vec_str| vec_str == s) {
316-
font::Family::Name(s)
317-
} else {
315+
FontFamily::Name(s) => FONTS.lock().map_or_else(
316+
|_| font::Family::default(),
317+
|mut vec_guard| {
318+
if !vec_guard.contains(s) {
318319
let leaked_str = &Box::leak(Box::new(String::from(s)))[..];
319-
vec_guard.push(&leaked_str);
320-
font::Family::Name(leaked_str)
320+
vec_guard.insert(&leaked_str);
321321
}
322-
} else {
323-
font::Family::default()
324-
}
325-
}
322+
323+
font::Family::Name(vec_guard.get(s).unwrap())
324+
},
325+
),
326326
},
327327
weight: match style.style() {
328328
FontStyle::Bold => font::Weight::Bold,

0 commit comments

Comments
 (0)