Skip to content

Commit

Permalink
string leek is stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Aug 25, 2023
1 parent e3b3ed8 commit 329af64
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/cargo/util/interning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ use std::str;
use std::sync::Mutex;
use std::sync::OnceLock;

fn leak(s: String) -> &'static str {
Box::leak(s.into_boxed_str())
}

static STRING_CACHE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -63,12 +59,9 @@ impl Eq for InternedString {}

impl InternedString {
pub fn new(str: &str) -> InternedString {
let mut cache = STRING_CACHE
.get_or_init(|| Default::default())
.lock()
.unwrap();
let mut cache = STRING_CACHE.get_or_init(Default::default).lock().unwrap();
let s = cache.get(str).cloned().unwrap_or_else(|| {
let s = leak(str.to_string());
let s = str.to_string().leak();
cache.insert(s);
s
});
Expand Down

0 comments on commit 329af64

Please sign in to comment.