Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string leek is stable #12559

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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