From 134d832f3323eb57f9c8882a99b9726dc40db95a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 25 Sep 2024 18:54:49 +0800 Subject: [PATCH] Avoid unwrap --- CHANGELOG.md | 1 + src/engine.rs | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45163b8c0..cbae22d49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Bug fixes * (Fuzzing) An integer-overflow bug from an inclusive range in the bits iterator is fixed. * (Fuzzing) An integer-underflow bug from an inclusive range is fixed. +* Copy strings if the strings interner is busy instead of panicing. New features ------------ diff --git a/src/engine.rs b/src/engine.rs index 771bff9f5..1937229dc 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -344,7 +344,10 @@ impl Engine { string: impl AsRef + Into, ) -> ImmutableString { match self.interned_strings { - Some(ref interner) => locked_write(interner).unwrap().get(string), + Some(ref interner) => match locked_write(interner) { + Some(mut cache) => cache.get(string), + None => string.into(), + }, None => string.into(), } } @@ -357,11 +360,12 @@ impl Engine { text: impl AsRef + Into, ) -> ImmutableString { match self.interned_strings { - Some(ref interner) => locked_write(interner).unwrap().get_with_mapper( - b'g', - |s| make_getter(s.as_ref()).into(), - text, - ), + Some(ref interner) => match locked_write(interner) { + Some(mut cache) => { + cache.get_with_mapper(b'g', |s| make_getter(s.as_ref()).into(), text) + } + None => make_getter(text.as_ref()).into(), + }, None => make_getter(text.as_ref()).into(), } } @@ -375,11 +379,12 @@ impl Engine { text: impl AsRef + Into, ) -> ImmutableString { match self.interned_strings { - Some(ref interner) => locked_write(interner).unwrap().get_with_mapper( - b's', - |s| make_setter(s.as_ref()).into(), - text, - ), + Some(ref interner) => match locked_write(interner) { + Some(mut cache) => { + cache.get_with_mapper(b's', |s| make_setter(s.as_ref()).into(), text) + } + None => make_setter(text.as_ref()).into(), + }, None => make_setter(text.as_ref()).into(), } }