godot_core/builtin/string/
string_name.rs

1/*
2 * Copyright (c) godot-rust; Bromeon and contributors.
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6 */
7
8use std::fmt;
9
10use godot_ffi as sys;
11use sys::{ffi_methods, ExtVariantType, GodotFfi};
12
13use crate::builtin::{inner, Encoding, GString, NodePath, Variant};
14use crate::meta::error::StringError;
15use crate::meta::AsArg;
16use crate::{impl_shared_string_api, meta};
17
18/// A string optimized for unique names.
19///
20/// StringNames are immutable strings designed for representing unique names. StringName ensures that only
21/// one instance of a given name exists.
22///
23/// # Ordering
24///
25/// In Godot, `StringName`s are **not** ordered lexicographically, and the ordering relation is **not** stable across multiple runs of your
26/// application. Therefore, this type does not implement `PartialOrd` and `Ord`, as it would be very easy to introduce bugs by accidentally
27/// relying on lexicographical ordering.
28///
29/// Instead, we provide [`transient_ord()`][Self::transient_ord] for ordering relations.
30///
31/// # Null bytes
32///
33/// Note that Godot ignores any bytes after a null-byte. This means that for instance `"hello, world!"` and  \
34/// `"hello, world!\0 ignored by Godot"` will be treated as the same string if converted to a `StringName`.
35///
36/// # All string types
37///
38/// | Intended use case | String type                                |
39/// |-------------------|--------------------------------------------|
40/// | General purpose   | [`GString`][crate::builtin::GString]       |
41/// | Interned names    | **`StringName`**                           |
42/// | Scene-node paths  | [`NodePath`][crate::builtin::NodePath]     |
43///
44/// # Godot docs
45///
46/// [`StringName` (stable)](https://docs.godotengine.org/en/stable/classes/class_stringname.html)
47// Currently we rely on `transparent` for `borrow_string_sys`.
48#[repr(transparent)]
49pub struct StringName {
50    opaque: sys::types::OpaqueStringName,
51}
52
53impl StringName {
54    fn from_opaque(opaque: sys::types::OpaqueStringName) -> Self {
55        Self { opaque }
56    }
57
58    /// Convert string from bytes with given encoding, returning `Err` on validation errors.
59    ///
60    /// Intermediate `NUL` characters are not accepted in Godot and always return `Err`.
61    ///
62    /// Some notes on the encodings:
63    /// - **Latin-1:** Since every byte is a valid Latin-1 character, no validation besides the `NUL` byte is performed.
64    ///   It is your responsibility to ensure that the input is meaningful under Latin-1.
65    /// - **ASCII**: Subset of Latin-1, which is additionally validated to be valid, non-`NUL` ASCII characters.
66    /// - **UTF-8**: The input is validated to be UTF-8.
67    ///
68    /// Specifying incorrect encoding is safe, but may result in unintended string values.
69    pub fn try_from_bytes(bytes: &[u8], encoding: Encoding) -> Result<Self, StringError> {
70        Self::try_from_bytes_with_nul_check(bytes, encoding, true)
71    }
72
73    /// Convert string from bytes with given encoding, returning `Err` on validation errors.
74    ///
75    /// Convenience function for [`try_from_bytes()`](Self::try_from_bytes); see its docs for more information.
76    ///
77    /// When called with `Encoding::Latin1`, this can be slightly more efficient than `try_from_bytes()`.
78    pub fn try_from_cstr(cstr: &std::ffi::CStr, encoding: Encoding) -> Result<Self, StringError> {
79        // Since Godot 4.2, we can directly short-circuit for Latin-1, which takes a null-terminated C string.
80        if encoding == Encoding::Latin1 {
81            // Note: CStr guarantees no intermediate NUL bytes, so we don't need to check for them.
82
83            let is_static = sys::conv::SYS_FALSE;
84            let s = unsafe {
85                Self::new_with_string_uninit(|string_ptr| {
86                    let ctor = sys::interface_fn!(string_name_new_with_latin1_chars);
87                    ctor(
88                        string_ptr,
89                        cstr.as_ptr() as *const std::ffi::c_char,
90                        is_static,
91                    );
92                })
93            };
94            return Ok(s);
95        }
96
97        Self::try_from_bytes_with_nul_check(cstr.to_bytes(), encoding, false)
98    }
99
100    fn try_from_bytes_with_nul_check(
101        bytes: &[u8],
102        encoding: Encoding,
103        check_nul: bool,
104    ) -> Result<Self, StringError> {
105        match encoding {
106            Encoding::Ascii => {
107                // ASCII is a subset of UTF-8, and UTF-8 has a more direct implementation than Latin-1; thus use UTF-8 via `From<&str>`.
108                if !bytes.is_ascii() {
109                    Err(StringError::new("invalid ASCII"))
110                } else if check_nul && bytes.contains(&0) {
111                    Err(StringError::new("intermediate NUL byte in ASCII string"))
112                } else {
113                    // SAFETY: ASCII is a subset of UTF-8 and was verified above.
114                    let ascii = unsafe { std::str::from_utf8_unchecked(bytes) };
115                    Ok(Self::from(ascii))
116                }
117            }
118            Encoding::Latin1 => {
119                // This branch is short-circuited if invoked for CStr, which uses `string_name_new_with_latin1_chars`
120                // (requires nul-termination). In general, fall back to GString conversion.
121                GString::try_from_bytes_with_nul_check(bytes, Encoding::Latin1, check_nul)
122                    .map(|s| Self::from(&s))
123            }
124            Encoding::Utf8 => {
125                // from_utf8() also checks for intermediate NUL bytes.
126                let utf8 = std::str::from_utf8(bytes);
127
128                utf8.map(StringName::from)
129                    .map_err(|e| StringError::with_source("invalid UTF-8", e))
130            }
131        }
132    }
133
134    /// Number of characters in the string.
135    ///
136    /// _Godot equivalent: `length`_
137    #[doc(alias = "length")]
138    pub fn len(&self) -> usize {
139        self.as_inner().length() as usize
140    }
141
142    /// Returns a 32-bit integer hash value representing the string.
143    pub fn hash(&self) -> u32 {
144        self.as_inner()
145            .hash()
146            .try_into()
147            .expect("Godot hashes are uint32_t")
148    }
149
150    meta::declare_arg_method! {
151        /// Use as argument for an [`impl AsArg<GString|NodePath>`][crate::meta::AsArg] parameter.
152        ///
153        /// This is a convenient way to convert arguments of similar string types.
154        ///
155        /// # Example
156        /// [`Node::set_name()`][crate::classes::Node::set_name] takes `GString`, let's pass a `StringName`:
157        /// ```no_run
158        /// # use godot::prelude::*;
159        /// let needle = StringName::from("str");
160        /// let haystack = GString::from("a long string");
161        /// let found = haystack.find(needle.arg());
162        /// ```
163    }
164
165    /// O(1), non-lexicographic, non-stable ordering relation.
166    ///
167    /// The result of the comparison is **not** lexicographic and **not** stable across multiple runs of your application.
168    ///
169    /// However, it is very fast. It doesn't depend on the length of the strings, but on the memory location of string names.
170    /// This can still be useful if you need to establish an ordering relation, but are not interested in the actual order of the strings
171    /// (example: binary search).
172    ///
173    /// For lexicographical ordering, convert to `GString` (significantly slower).
174    pub fn transient_ord(&self) -> TransientStringNameOrd<'_> {
175        TransientStringNameOrd(self)
176    }
177
178    ffi_methods! {
179        type sys::GDExtensionStringNamePtr = *mut Opaque;
180
181        // Note: unlike from_sys, from_string_sys does not default-construct instance first. Typical usage in C++ is placement new.
182        fn new_from_string_sys = new_from_sys;
183        fn new_with_string_uninit = new_with_uninit;
184        fn string_sys = sys;
185        fn string_sys_mut = sys_mut;
186    }
187
188    /// Consumes self and turns it into a sys-ptr, should be used together with [`from_owned_string_sys`](Self::from_owned_string_sys).
189    ///
190    /// This will leak memory unless `from_owned_string_sys` is called on the returned pointer.
191    pub(crate) fn into_owned_string_sys(self) -> sys::GDExtensionStringNamePtr {
192        sys::static_assert_eq_size_align!(StringName, sys::types::OpaqueStringName);
193
194        let leaked = Box::into_raw(Box::new(self));
195        leaked.cast()
196    }
197
198    /// Creates a `StringName` from a sys-ptr without incrementing the refcount.
199    ///
200    /// # Safety
201    ///
202    /// * Must only be used on a pointer returned from a call to [`into_owned_string_sys`](Self::into_owned_string_sys).
203    /// * Must not be called more than once on the same pointer.
204    #[deny(unsafe_op_in_unsafe_fn)]
205    pub(crate) unsafe fn from_owned_string_sys(ptr: sys::GDExtensionStringNamePtr) -> Self {
206        sys::static_assert_eq_size_align!(StringName, sys::types::OpaqueStringName);
207
208        let ptr = ptr.cast::<Self>();
209
210        // SAFETY: `ptr` was returned from a call to `into_owned_string_sys`, which means it was created by a call to
211        // `Box::into_raw`, thus we can use `Box::from_raw` here. Additionally, this is only called once on this pointer.
212        let boxed = unsafe { Box::from_raw(ptr) };
213        *boxed
214    }
215
216    /// Convert a `StringName` sys pointer to a reference with unbounded lifetime.
217    ///
218    /// # Safety
219    ///
220    /// `ptr` must point to a live `StringName` for the duration of `'a`.
221    pub(crate) unsafe fn borrow_string_sys<'a>(
222        ptr: sys::GDExtensionConstStringNamePtr,
223    ) -> &'a StringName {
224        sys::static_assert_eq_size_align!(StringName, sys::types::OpaqueStringName);
225        &*(ptr.cast::<StringName>())
226    }
227
228    /// Convert a `StringName` sys pointer to a mutable reference with unbounded lifetime.
229    ///
230    /// # Safety
231    ///
232    /// - `ptr` must point to a live `StringName` for the duration of `'a`.
233    /// - Must be exclusive - no other reference to given `StringName` instance can exist for the duration of `'a`.
234    pub(crate) unsafe fn borrow_string_sys_mut<'a>(
235        ptr: sys::GDExtensionStringNamePtr,
236    ) -> &'a mut StringName {
237        sys::static_assert_eq_size_align!(StringName, sys::types::OpaqueStringName);
238        &mut *(ptr.cast::<StringName>())
239    }
240
241    #[doc(hidden)]
242    pub fn as_inner(&self) -> inner::InnerStringName<'_> {
243        inner::InnerStringName::from_outer(self)
244    }
245
246    #[doc(hidden)] // Private for now. Needs API discussion, also regarding overlap with try_from_cstr().
247    pub fn __cstr(c_str: &'static std::ffi::CStr) -> Self {
248        // This used to be set to true, but `p_is_static` parameter in Godot should only be enabled if the result is indeed stored
249        // in a static. See discussion in https://github.com/godot-rust/gdext/pull/1316. We may unify this into a regular constructor,
250        // or provide a dedicated StringName cache (similar to ClassId cache) in the future, which would be freed on shutdown.
251        let is_static = false;
252
253        Self::__cstr_with_static(c_str, is_static)
254    }
255
256    /// Creates a `StringName` from a static ASCII/Latin-1 `c"string"`.
257    ///
258    /// If `is_static` is true, avoids unnecessary copies and allocations and directly uses the backing buffer. However, this must
259    /// be stored in an actual `static` to not cause leaks/error messages with Godot. For literals, use `is_static=false`.
260    ///
261    /// Note that while Latin-1 encoding is the most common encoding for c-strings, it isn't a requirement. So if your c-string
262    /// uses a different encoding (e.g. UTF-8), it is possible that some characters will not show up as expected.
263    ///
264    /// # Safety
265    /// `c_str` must be a static c-string that remains valid for the entire program duration.
266    ///
267    /// # Example
268    /// ```no_run
269    /// use godot::builtin::StringName;
270    ///
271    /// // '±' is a Latin-1 character with codepoint 0xB1. Note that this is not UTF-8, where it would need two bytes.
272    /// let sname = StringName::__cstr(c"\xb1 Latin-1 string");
273    /// ```
274    #[doc(hidden)] // Private for now. Needs API discussion, also regarding overlap with try_from_cstr().
275    pub fn __cstr_with_static(c_str: &'static std::ffi::CStr, is_static: bool) -> Self {
276        // SAFETY: c_str is nul-terminated and remains valid for entire program duration.
277        unsafe {
278            Self::new_with_string_uninit(|ptr| {
279                sys::interface_fn!(string_name_new_with_latin1_chars)(
280                    ptr,
281                    c_str.as_ptr(),
282                    sys::conv::bool_to_sys(is_static),
283                )
284            })
285        }
286    }
287}
288
289// SAFETY:
290// - `move_return_ptr`
291//   Nothing special needs to be done beyond a `std::mem::swap` when returning a StringName.
292//   So we can just use `ffi_methods`.
293//
294// - `from_arg_ptr`
295//   StringNames are properly initialized through a `from_sys` call, but the ref-count should be
296//   incremented as that is the callee's responsibility. Which we do by calling
297//   `std::mem::forget(string_name.clone())`.
298unsafe impl GodotFfi for StringName {
299    const VARIANT_TYPE: ExtVariantType = ExtVariantType::Concrete(sys::VariantType::STRING_NAME);
300
301    ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; .. }
302}
303
304meta::impl_godot_as_self!(StringName: ByRef);
305
306impl_builtin_traits! {
307    for StringName {
308        Default => string_name_construct_default;
309        Clone => string_name_construct_copy;
310        Drop => string_name_destroy;
311        Eq => string_name_operator_equal;
312        // Do not provide PartialOrd or Ord. Even though Godot provides a `operator <`, it is non-lexicographic and non-deterministic
313        // (based on pointers). See transient_ord() method.
314        Hash;
315    }
316}
317
318impl_shared_string_api! {
319    builtin: StringName,
320    find_builder: ExStringNameFind,
321    split_builder: ExStringNameSplit,
322}
323
324impl fmt::Display for StringName {
325    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
326        let s = GString::from(self);
327        <GString as fmt::Display>::fmt(&s, f)
328    }
329}
330
331/// Uses literal syntax from GDScript: `&"string_name"`
332impl fmt::Debug for StringName {
333    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
334        let string = GString::from(self);
335        write!(f, "&\"{string}\"")
336    }
337}
338
339// SAFETY: StringName is immutable once constructed. Shared references can thus not undergo mutation.
340unsafe impl Sync for StringName {}
341
342// SAFETY: StringName is immutable once constructed. Also, its inc-ref/dec-ref operations are mutex-protected in Godot.
343// That is, it's safe to construct a StringName on thread A and destroy it on thread B.
344unsafe impl Send for StringName {}
345
346// ----------------------------------------------------------------------------------------------------------------------------------------------
347// Conversion from/into other string-types
348
349impl_rust_string_conv!(StringName);
350
351impl From<&str> for StringName {
352    fn from(string: &str) -> Self {
353        let utf8 = string.as_bytes();
354
355        // SAFETY: Rust guarantees validity and range of string.
356        unsafe {
357            Self::new_with_string_uninit(|ptr| {
358                sys::interface_fn!(string_name_new_with_utf8_chars_and_len)(
359                    ptr,
360                    utf8.as_ptr() as *const std::ffi::c_char,
361                    utf8.len() as i64,
362                );
363            })
364        }
365    }
366}
367
368impl From<&String> for StringName {
369    fn from(value: &String) -> Self {
370        value.as_str().into()
371    }
372}
373
374impl From<&GString> for StringName {
375    /// See also [`GString::to_string_name()`].
376    fn from(string: &GString) -> Self {
377        unsafe {
378            Self::new_with_uninit(|self_ptr| {
379                let ctor = sys::builtin_fn!(string_name_from_string);
380                let args = [string.sys()];
381                ctor(self_ptr, args.as_ptr());
382            })
383        }
384    }
385}
386
387impl From<&NodePath> for StringName {
388    fn from(path: &NodePath) -> Self {
389        Self::from(&GString::from(path))
390    }
391}
392
393// ----------------------------------------------------------------------------------------------------------------------------------------------
394// Ordering
395
396/// Type that implements `Ord` for `StringNames`.
397///
398/// See [`StringName::transient_ord()`].
399pub struct TransientStringNameOrd<'a>(&'a StringName);
400
401impl PartialEq for TransientStringNameOrd<'_> {
402    fn eq(&self, other: &Self) -> bool {
403        self.0 == other.0
404    }
405}
406
407impl Eq for TransientStringNameOrd<'_> {}
408
409impl PartialOrd for TransientStringNameOrd<'_> {
410    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
411        Some(self.cmp(other))
412    }
413}
414
415impl Ord for TransientStringNameOrd<'_> {
416    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
417        // SAFETY: builtin operator provided by Godot.
418        let op_less = |lhs, rhs| unsafe {
419            let mut result = false;
420            sys::builtin_call! {
421                string_name_operator_less(lhs, rhs, result.sys_mut())
422            }
423            result
424        };
425
426        let self_ptr = self.0.sys();
427        let other_ptr = other.0.sys();
428
429        if op_less(self_ptr, other_ptr) {
430            std::cmp::Ordering::Less
431        } else if op_less(other_ptr, self_ptr) {
432            std::cmp::Ordering::Greater
433        } else if self.eq(other) {
434            std::cmp::Ordering::Equal
435        } else {
436            panic!(
437                "Godot provides inconsistent StringName ordering for \"{}\" and \"{}\"",
438                self.0, other.0
439            );
440        }
441    }
442}
443
444// ----------------------------------------------------------------------------------------------------------------------------------------------
445// serde support
446
447#[cfg(feature = "serde")] #[cfg_attr(published_docs, doc(cfg(feature = "serde")))]
448mod serialize {
449    use std::fmt::Formatter;
450
451    use serde::de::{Error, Visitor};
452    use serde::{Deserialize, Deserializer, Serialize, Serializer};
453
454    use super::*;
455
456    // For "Available on crate feature `serde`" in docs. Cannot be inherited from module. Also does not support #[derive] (e.g. in Vector2).
457    #[cfg_attr(published_docs, doc(cfg(feature = "serde")))]
458    impl Serialize for StringName {
459        #[inline]
460        fn serialize<S>(
461            &self,
462            serializer: S,
463        ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
464        where
465            S: Serializer,
466        {
467            serializer.serialize_str(&self.to_string())
468        }
469    }
470
471    #[cfg_attr(published_docs, doc(cfg(feature = "serde")))]
472    impl<'de> Deserialize<'de> for StringName {
473        #[inline]
474        fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
475        where
476            D: Deserializer<'de>,
477        {
478            struct StringNameVisitor;
479            impl Visitor<'_> for StringNameVisitor {
480                type Value = StringName;
481
482                fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
483                    formatter.write_str("a StringName")
484                }
485
486                fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
487                where
488                    E: Error,
489                {
490                    Ok(StringName::from(s))
491                }
492            }
493
494            deserializer.deserialize_str(StringNameVisitor)
495        }
496    }
497}
498
499// TODO(v0.4.x): consider re-exposing in public API. Open questions: thread-safety, performance, memory leaks, global overhead.
500// Possibly in a more general StringName cache, similar to ClassId. See https://github.com/godot-rust/gdext/pull/1316.
501/// Creates and gets a reference to a static `StringName` from a ASCII/Latin-1 `c"string"`.
502///
503/// This is the fastest way to create a StringName repeatedly, with the result being cached and never released, like `SNAME` in Godot source code. Suitable for scenarios where high performance is required.
504#[macro_export]
505macro_rules! static_sname {
506    ($str:literal) => {{
507        use std::sync::OnceLock;
508
509        let c_str: &'static std::ffi::CStr = $str;
510        static SNAME: OnceLock<StringName> = OnceLock::new();
511        SNAME.get_or_init(|| StringName::__cstr_with_static(c_str, true))
512    }};
513}