Skip to content

Commit a68fa68

Browse files
committed
make and use static interned storage in one fn
1 parent e98f702 commit a68fa68

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/cargo/util/interning.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use std::str;
1313
use std::sync::Mutex;
1414
use std::sync::OnceLock;
1515

16-
static STRING_CACHE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
16+
fn interned_storage() -> std::sync::MutexGuard<'static, HashSet<&'static str>> {
17+
static STRING_CACHE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
18+
STRING_CACHE.get_or_init(Default::default).lock().unwrap()
19+
}
1720

1821
#[derive(Clone, Copy)]
1922
pub struct InternedString {
@@ -60,8 +63,8 @@ impl Eq for InternedString {}
6063

6164
impl InternedString {
6265
pub fn new(str: &str) -> InternedString {
63-
let mut cache = STRING_CACHE.get_or_init(Default::default).lock().unwrap();
64-
let s = cache.get(str).cloned().unwrap_or_else(|| {
66+
let mut cache = interned_storage();
67+
let s = cache.get(str).copied().unwrap_or_else(|| {
6568
let s = str.to_string().leak();
6669
cache.insert(s);
6770
s

0 commit comments

Comments
 (0)