Skip to content

Commit 172f597

Browse files
authored
feat: FastString - ability to unsafely specify if string is ASCII or not (#943)
1 parent 2b00d44 commit 172f597

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

core/fast_string.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ enum FastStringInner {
157157
}
158158

159159
impl FastString {
160-
/// Create a [`FastString`] from a static string. The string may contain non-ASCII characters, and if
161-
/// so, will take the slower path when used in v8.
160+
/// Create a [`FastString`] from a static string. The string may contain
161+
/// non-ASCII characters, and if so, will take the slower path when used
162+
/// in v8.
162163
pub const fn from_static(s: &'static str) -> Self {
163164
if s.is_ascii() {
164165
Self {
@@ -171,6 +172,47 @@ impl FastString {
171172
}
172173
}
173174

175+
/// Create a [`FastString`] from a static string that is known to contain
176+
/// only ASCII characters.
177+
///
178+
/// Note: This function is deliberately not `const fn`. Use `from_static`
179+
/// in const contexts.
180+
///
181+
/// # Safety
182+
///
183+
/// It is unsafe to specify a non-ASCII string here because this will be
184+
/// referenced in an external one byte static string in v8, which requires
185+
/// the data be Latin-1 or ASCII.
186+
///
187+
/// This should only be used in scenarios where you know a string is ASCII
188+
/// and you want to avoid the performance overhead of checking if a string
189+
/// is ASCII that `from_static` does.
190+
pub unsafe fn from_ascii_static_unchecked(s: &'static str) -> Self {
191+
debug_assert!(
192+
s.is_ascii(),
193+
"use `from_non_ascii_static_unsafe` for non-ASCII strings",
194+
);
195+
Self {
196+
inner: FastStringInner::StaticAscii(s),
197+
}
198+
}
199+
200+
/// Create a [`FastString`] from a static string that may contain non-ASCII
201+
/// characters.
202+
///
203+
/// This should only be used in scenarios where you know a string is not ASCII
204+
/// and you want to avoid the performance overhead of checking if the string
205+
/// is ASCII that `from_static` does.
206+
///
207+
/// Note: This function is deliberately not `const fn`. Use `from_static`
208+
/// in const contexts. This function is not unsafe because using this with
209+
/// an ascii string will just not be as optimal for performance.
210+
pub fn from_non_ascii_static(s: &'static str) -> Self {
211+
Self {
212+
inner: FastStringInner::Static(s),
213+
}
214+
}
215+
174216
/// Returns a static string from this `FastString`, if available.
175217
pub fn as_static_str(&self) -> Option<&'static str> {
176218
match self.inner {
@@ -181,8 +223,9 @@ impl FastString {
181223
}
182224
}
183225

184-
/// Creates a cheap copy of this [`FastString`], potentially transmuting it to a faster form. Note that this
185-
/// is not a clone operation as it consumes the old [`FastString`].
226+
/// Creates a cheap copy of this [`FastString`], potentially transmuting it
227+
/// to a faster form. Note that this is not a clone operation as it consumes
228+
/// the old [`FastString`].
186229
pub fn into_cheap_copy(self) -> (Self, Self) {
187230
match self.inner {
188231
FastStringInner::Owned(s) => {

0 commit comments

Comments
 (0)