Skip to content

Commit 789e8cb

Browse files
committed
Unexpose static_name! for now
1 parent 9d5cb63 commit 789e8cb

File tree

4 files changed

+13
-28
lines changed

4 files changed

+13
-28
lines changed

godot-core/src/builtin/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub mod __prelude_reexport {
4141
pub use rect2i::*;
4242
pub use rid::*;
4343
pub use signal::*;
44-
pub use string::{static_name, Encoding, GString, NodePath, StringName};
44+
pub use string::{Encoding, GString, NodePath, StringName};
4545
pub use transform2d::*;
4646
pub use transform3d::*;
4747
pub use variant::*;
@@ -54,6 +54,9 @@ pub mod __prelude_reexport {
5454
#[allow(deprecated)]
5555
#[rustfmt::skip] // Do not reorder.
5656
pub use crate::dict;
57+
58+
#[cfg(feature = "trace")] // Test only.
59+
pub use crate::static_sname;
5760
}
5861

5962
pub use __prelude_reexport::*;

godot-core/src/builtin/string/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub use string_name::*;
2020
use crate::meta;
2121
use crate::meta::error::ConvertError;
2222
use crate::meta::{FromGodot, GodotConvert, ToGodot};
23-
pub use crate::static_name;
2423

2524
impl GodotConvert for &str {
2625
type Via = GString;

godot-core/src/builtin/string/string_name.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ use crate::{impl_shared_string_api, meta};
3434
/// Note that Godot ignores any bytes after a null-byte. This means that for instance `"hello, world!"` and \
3535
/// `"hello, world!\0 ignored by Godot"` will be treated as the same string if converted to a `StringName`.
3636
///
37-
/// # Performance
38-
///
39-
/// The fastest way to create string names is using [`static_name!`][crate::builtin::static_name], which creates a static cached `StringName` from null-terminated C-string literals such as `c"MyClass"`. These can be used directly by Godot without conversion. The encoding is limited to Latin-1, however. See the corresponding
40-
/// [`From<&CStr>` impl](#impl-From<%26CStr>-for-StringName).
41-
///
4237
/// # All string types
4338
///
4439
/// | Intended use case | String type |
@@ -492,29 +487,17 @@ mod serialize {
492487
}
493488
}
494489

490+
// TODO(v0.4.x): consider re-exposing in public API. Open questions: thread-safety, performance, memory leaks, global overhead.
495491
/// Creates and gets a reference to a static `StringName` from a ASCII/Latin-1 `c"string"`.
496492
///
497493
/// 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.
498494
#[macro_export]
499-
macro_rules! static_name {
495+
macro_rules! static_sname {
500496
($str:literal) => {{
501497
use std::sync::OnceLock;
502498

503-
use godot::sys;
504-
505499
let c_str: &'static std::ffi::CStr = $str;
506500
static SNAME: OnceLock<StringName> = OnceLock::new();
507-
SNAME.get_or_init(|| {
508-
// SAFETY: c_str is nul-terminated and remains valid for entire program duration.
509-
unsafe {
510-
StringName::new_with_string_uninit(|ptr| {
511-
sys::interface_fn!(string_name_new_with_latin1_chars)(
512-
ptr,
513-
c_str.as_ptr(),
514-
sys::conv::SYS_TRUE, // p_is_static
515-
)
516-
})
517-
}
518-
})
501+
SNAME.get_or_init(|| StringName::__static_cstr(c_str))
519502
}};
520503
}

itest/rust/src/builtin_tests/string/string_name_test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use std::collections::HashSet;
99

10-
use godot::builtin::{static_name, Encoding, GString, NodePath, StringName};
10+
use godot::builtin::{static_sname, Encoding, GString, NodePath, StringName};
1111

1212
use crate::framework::{assert_eq_self, itest};
1313

@@ -135,24 +135,24 @@ fn string_name_from_cstr() {
135135
}
136136

137137
#[itest]
138-
fn string_name_static_name() {
139-
let a = static_name!(c"pure ASCII\t[~]").clone();
138+
fn string_name_static_sname() {
139+
let a = static_sname!(c"pure ASCII\t[~]").clone();
140140
let b = StringName::from("pure ASCII\t[~]");
141141

142142
assert_eq!(a, b);
143143

144144
let a1 = a.clone();
145-
let a2 = static_name!(c"pure ASCII\t[~]").clone();
145+
let a2 = static_sname!(c"pure ASCII\t[~]").clone();
146146

147147
assert_eq!(a, a1);
148148
assert_eq!(a1, a2);
149149

150-
let a = static_name!(c"\xB1").clone();
150+
let a = static_sname!(c"\xB1").clone();
151151
let b = StringName::from("±");
152152

153153
assert_eq!(a, b);
154154

155-
let a = static_name!(c"Latin-1 \xA3 \xB1 text \xBE").clone();
155+
let a = static_sname!(c"Latin-1 \xA3 \xB1 text \xBE").clone();
156156
let b = StringName::from("Latin-1 £ ± text ¾");
157157

158158
assert_eq!(a, b);

0 commit comments

Comments
 (0)