@@ -157,8 +157,9 @@ enum FastStringInner {
157
157
}
158
158
159
159
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.
162
163
pub const fn from_static ( s : & ' static str ) -> Self {
163
164
if s. is_ascii ( ) {
164
165
Self {
@@ -171,6 +172,47 @@ impl FastString {
171
172
}
172
173
}
173
174
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
+
174
216
/// Returns a static string from this `FastString`, if available.
175
217
pub fn as_static_str ( & self ) -> Option < & ' static str > {
176
218
match self . inner {
@@ -181,8 +223,9 @@ impl FastString {
181
223
}
182
224
}
183
225
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`].
186
229
pub fn into_cheap_copy ( self ) -> ( Self , Self ) {
187
230
match self . inner {
188
231
FastStringInner :: Owned ( s) => {
0 commit comments