Skip to content

Commit d3328df

Browse files
committed
Define CowStr type alias (internal for now)
1 parent 5d4e1a6 commit d3328df

File tree

5 files changed

+16
-19
lines changed

5 files changed

+16
-19
lines changed

godot-core/src/builtin/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ pub use crate::sys::VariantType;
2222
#[allow(deprecated)] // dict
2323
pub use crate::{array, dict, real, reals, varray, vdict};
2424

25+
/// Abbreviation for occasionally used _owned or borrowed string_.
26+
#[doc(hidden)]
27+
pub type CowStr = std::borrow::Cow<'static, str>;
28+
2529
#[doc(hidden)]
2630
pub mod __prelude_reexport {
2731
#[rustfmt::skip] // Do not reorder.

godot-core/src/meta/class_id.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl ClassId {
156156
}
157157

158158
/// Returns an owned or borrowed `str` representing the class name.
159-
pub fn to_cow_str(&self) -> Cow<'static, str> {
159+
pub fn to_cow_str(&self) -> CowStr {
160160
let cache = CLASS_ID_CACHE.lock();
161161
let entry = cache.get_entry(self.global_index as usize);
162162
entry.rust_str.clone()
@@ -206,12 +206,12 @@ impl fmt::Debug for ClassId {
206206
///
207207
/// `StringName` needs to be lazy-initialized because the Godot binding may not be initialized yet.
208208
struct ClassIdEntry {
209-
rust_str: Cow<'static, str>,
209+
rust_str: CowStr,
210210
godot_str: OnceCell<StringName>,
211211
}
212212

213213
impl ClassIdEntry {
214-
fn new(rust_str: Cow<'static, str>) -> Self {
214+
fn new(rust_str: CowStr) -> Self {
215215
Self {
216216
rust_str,
217217
godot_str: OnceCell::new(),
@@ -257,7 +257,7 @@ impl ClassIdCache {
257257
/// If `expect_first` is true and the string is already present in the cache.
258258
fn insert_class_id(
259259
&mut self,
260-
source: Cow<'static, str>,
260+
source: CowStr,
261261
type_id: Option<TypeId>,
262262
expect_first: bool,
263263
) -> ClassId {

godot-core/src/registry/signal/connect_handle.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8-
use std::borrow::Cow;
9-
10-
use crate::builtin::Callable;
8+
use crate::builtin::{Callable, CowStr};
119
use crate::classes::Object;
1210
use crate::obj::Gd;
1311
use crate::sys;
@@ -20,15 +18,15 @@ use crate::sys;
2018
/// Connections managed by a handle can be disconnected using [`disconnect()`][Self::disconnect].
2119
pub struct ConnectHandle {
2220
receiver_object: Gd<Object>,
23-
signal_name: Cow<'static, str>,
21+
signal_name: CowStr,
2422
callable: Callable,
2523
}
2624

2725
impl ConnectHandle {
2826
// Should only be invoked by connect_* methods.
2927
pub(super) fn new(
3028
receiver_object: Gd<Object>,
31-
signal_name: Cow<'static, str>,
29+
signal_name: CowStr,
3230
callable: Callable,
3331
) -> Self {
3432
Self {

godot-core/src/registry/signal/typed_signal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::marker::PhantomData;
1010
use std::ops::DerefMut;
1111

1212
use super::{make_callable_name, make_godot_fn, ConnectBuilder, ConnectHandle, SignalObject};
13-
use crate::builtin::{Callable, Variant};
13+
use crate::builtin::{Callable, CowStr, Variant};
1414
use crate::classes::object::ConnectFlags;
1515
use crate::meta;
1616
use crate::meta::{InParamTuple, ObjectToOwned, UniformObjectDeref};
@@ -52,7 +52,7 @@ use crate::registry::signal::signal_receiver::{IndirectSignalReceiver, SignalRec
5252
pub struct TypedSignal<'c, C: WithSignals, Ps> {
5353
/// In Godot, valid signals (unlike funcs) are _always_ declared in a class and become part of each instance. So there's always an object.
5454
object: C::__SignalObj<'c>,
55-
name: Cow<'static, str>,
55+
name: CowStr,
5656
_signature: PhantomData<Ps>,
5757
}
5858

itest/rust/src/object_tests/class_name_test.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8-
use std::borrow::Cow;
9-
10-
use godot::builtin::{GString, StringName};
8+
use godot::builtin::{CowStr, GString, StringName};
119
use godot::meta::ClassId;
1210
use godot::obj::bounds::implement_godot_bounds;
1311
use godot::obj::GodotClass;
@@ -48,7 +46,7 @@ fn class_name_godotclass() {
4846
assert_eq!(a.to_string(), "A");
4947
assert_eq!(a.to_gstring(), GString::from("A"));
5048
assert_eq!(a.to_string_name(), StringName::from("A"));
51-
assert_eq!(a.to_cow_str(), Cow::<'static, str>::Owned("A".to_string()));
49+
assert_eq!(a.to_cow_str(), CowStr::Owned("A".to_string()));
5250
}
5351

5452
#[cfg(since_api = "4.4")]
@@ -63,10 +61,7 @@ fn class_name_godotclass_unicode() {
6361
assert_eq!(a.to_string(), "统一码");
6462
assert_eq!(a.to_gstring(), GString::from("统一码"));
6563
assert_eq!(a.to_string_name(), StringName::from("统一码"));
66-
assert_eq!(
67-
a.to_cow_str(),
68-
Cow::<'static, str>::Owned("统一码".to_string())
69-
);
64+
assert_eq!(a.to_cow_str(), CowStr::Owned("统一码".to_string()));
7065

7166
let b = ClassId::__dynamic("统一码");
7267
assert_eq!(a, b);

0 commit comments

Comments
 (0)