Skip to content

Commit c55eef0

Browse files
committed
Remove static lifetime in StringName::from(&CStr)
1 parent 07c34e7 commit c55eef0

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ use crate::{impl_shared_string_api, meta};
3535
///
3636
/// # Performance
3737
///
38-
/// The fastest way to create string names is by using null-terminated C-string literals such as `c"MyClass"`. These have `'static` lifetime and
39-
/// can be used directly by Godot, without allocation or conversion. The encoding is limited to Latin-1, however. See the corresponding
40-
/// [`From<&'static CStr>` impl](#impl-From<%26CStr>-for-StringName).
38+
/// The fastest way to create string names is by using 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
39+
/// [`From<&CStr>` impl](#impl-From<%26CStr>-for-StringName).
4140
///
4241
/// # All string types
4342
///
@@ -249,11 +248,6 @@ impl StringName {
249248
pub fn as_inner(&self) -> inner::InnerStringName<'_> {
250249
inner::InnerStringName::from_outer(self)
251250
}
252-
253-
/// Increment ref-count. This may leak memory if used wrongly.
254-
fn inc_ref(&self) {
255-
std::mem::forget(self.clone());
256-
}
257251
}
258252

259253
// SAFETY:
@@ -384,8 +378,8 @@ impl From<NodePath> for StringName {
384378
}
385379
}
386380

387-
impl From<&'static std::ffi::CStr> for StringName {
388-
/// Creates a `StringName` from a static ASCII/Latin-1 `c"string"`.
381+
impl From<&std::ffi::CStr> for StringName {
382+
/// Creates a `StringName` from a ASCII/Latin-1 `c"string"`.
389383
///
390384
/// This avoids unnecessary copies and allocations and directly uses the backing buffer. Useful for literals.
391385
///
@@ -399,23 +393,17 @@ impl From<&'static std::ffi::CStr> for StringName {
399393
/// // '±' is a Latin-1 character with codepoint 0xB1. Note that this is not UTF-8, where it would need two bytes.
400394
/// let sname = StringName::from(c"\xb1 Latin-1 string");
401395
/// ```
402-
fn from(c_str: &'static std::ffi::CStr) -> Self {
396+
fn from(c_str: &std::ffi::CStr) -> Self {
403397
// SAFETY: c_str is nul-terminated and remains valid for entire program duration.
404-
let result = unsafe {
398+
unsafe {
405399
Self::new_with_string_uninit(|ptr| {
406400
sys::interface_fn!(string_name_new_with_latin1_chars)(
407401
ptr,
408402
c_str.as_ptr(),
409-
sys::conv::SYS_TRUE, // p_is_static
403+
sys::conv::SYS_FALSE, // p_is_static
410404
)
411405
})
412-
};
413-
414-
// StringName expects that the destructor is not invoked on static instances (or only at global exit; see SNAME(..) macro in Godot).
415-
// According to testing with godot4 --verbose, there is no mention of "Orphan StringName" at shutdown when incrementing the ref-count,
416-
// so this should not leak memory.
417-
result.inc_ref();
418-
result
406+
}
419407
}
420408
}
421409

0 commit comments

Comments
 (0)