Add PicoStr::get

This commit is contained in:
Laurenz 2025-07-15 10:43:53 +02:00
parent 9a6268050f
commit 830611ceef

View File

@ -65,6 +65,23 @@ impl PicoStr {
id
}
/// Try to create a `PicoStr`, but don't intern it if it does not exist yet.
///
/// This is useful to try to compare against one or multiple `PicoStr`
/// without interning needlessly.
///
/// Will always return `Some(_)` if the string can be represented inline.
pub fn get(string: &str) -> Option<PicoStr> {
// Try to use bitcode or exception representations.
if let Ok(value) = PicoStr::try_constant(string) {
return Some(value);
}
// Try to find an existing entry that we can reuse.
let interner = INTERNER.read().unwrap();
interner.seen.get(string).copied()
}
/// Creates a compile-time constant `PicoStr`.
///
/// Should only be used in const contexts because it can panic.