|
4 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this
|
5 | 5 | * file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
6 | 6 | */
|
| 7 | + |
7 | 8 | use std::fmt;
|
8 | 9 |
|
9 | 10 | use godot_ffi as sys;
|
@@ -248,6 +249,37 @@ impl StringName {
|
248 | 249 | pub fn as_inner(&self) -> inner::InnerStringName<'_> {
|
249 | 250 | inner::InnerStringName::from_outer(self)
|
250 | 251 | }
|
| 252 | + |
| 253 | + /// Creates a `StringName` from a static ASCII/Latin-1 `c"string"`. |
| 254 | + /// |
| 255 | + /// This avoids unnecessary copies and allocations and directly uses the backing buffer. Useful for literals. |
| 256 | + /// |
| 257 | + /// Note that while Latin-1 encoding is the most common encoding for c-strings, it isn't a requirement. So if your c-string |
| 258 | + /// uses a different encoding (e.g. UTF-8), it is possible that some characters will not show up as expected. |
| 259 | + /// |
| 260 | + /// # Safety |
| 261 | + /// `c_str` must be a static c-string that remains valid for the entire program duration. |
| 262 | + /// |
| 263 | + /// # Example |
| 264 | + /// ```no_run |
| 265 | + /// use godot::builtin::StringName; |
| 266 | + /// |
| 267 | + /// // '±' is a Latin-1 character with codepoint 0xB1. Note that this is not UTF-8, where it would need two bytes. |
| 268 | + /// let sname = StringName::__static_cstr(c"\xb1 Latin-1 string"); |
| 269 | + /// ``` |
| 270 | + #[doc(hidden)] // Private for now. Needs API discussion, also regarding overlap with try_from_cstr(). |
| 271 | + pub fn __static_cstr(c_str: &'static std::ffi::CStr) -> Self { |
| 272 | + // SAFETY: c_str is nul-terminated and remains valid for entire program duration. |
| 273 | + unsafe { |
| 274 | + Self::new_with_string_uninit(|ptr| { |
| 275 | + sys::interface_fn!(string_name_new_with_latin1_chars)( |
| 276 | + ptr, |
| 277 | + c_str.as_ptr(), |
| 278 | + sys::conv::SYS_TRUE, // p_is_static |
| 279 | + ) |
| 280 | + }) |
| 281 | + } |
| 282 | + } |
251 | 283 | }
|
252 | 284 |
|
253 | 285 | // SAFETY:
|
@@ -354,35 +386,6 @@ impl From<&NodePath> for StringName {
|
354 | 386 | }
|
355 | 387 | }
|
356 | 388 |
|
357 |
| -impl From<&std::ffi::CStr> for StringName { |
358 |
| - /// Creates a `StringName` from a ASCII/Latin-1 `c"string"`. |
359 |
| - /// |
360 |
| - /// This avoids unnecessary copies and allocations and directly uses the backing buffer. Useful for literals. |
361 |
| - /// |
362 |
| - /// Note that while Latin-1 encoding is the most common encoding for c-strings, it isn't a requirement. So if your c-string |
363 |
| - /// uses a different encoding (e.g. UTF-8), it is possible that some characters will not show up as expected. |
364 |
| - /// |
365 |
| - /// # Example |
366 |
| - /// ```no_run |
367 |
| - /// use godot::builtin::StringName; |
368 |
| - /// |
369 |
| - /// // '±' is a Latin-1 character with codepoint 0xB1. Note that this is not UTF-8, where it would need two bytes. |
370 |
| - /// let sname = StringName::from(c"\xb1 Latin-1 string"); |
371 |
| - /// ``` |
372 |
| - fn from(c_str: &std::ffi::CStr) -> Self { |
373 |
| - // SAFETY: c_str is nul-terminated and remains valid for entire program duration. |
374 |
| - unsafe { |
375 |
| - Self::new_with_string_uninit(|ptr| { |
376 |
| - sys::interface_fn!(string_name_new_with_latin1_chars)( |
377 |
| - ptr, |
378 |
| - c_str.as_ptr(), |
379 |
| - sys::conv::SYS_FALSE, // p_is_static |
380 |
| - ) |
381 |
| - }) |
382 |
| - } |
383 |
| - } |
384 |
| -} |
385 |
| - |
386 | 389 | // ----------------------------------------------------------------------------------------------------------------------------------------------
|
387 | 390 | // Ordering
|
388 | 391 |
|
|
0 commit comments