Skip to content

Commit a834d58

Browse files
committed
Add StringName == &str equality operator
1 parent 6f97e3c commit a834d58

File tree

13 files changed

+39
-25
lines changed

13 files changed

+39
-25
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ impl_shared_string_api! {
348348
split_builder: ExStringNameSplit,
349349
}
350350

351+
// ----------------------------------------------------------------------------------------------------------------------------------------------
352+
// Comparison with Rust strings
353+
354+
// API design: see PartialEq for GString.
355+
impl PartialEq<&str> for StringName {
356+
fn eq(&self, other: &&str) -> bool {
357+
self.chars().iter().copied().eq(other.chars())
358+
}
359+
}
360+
351361
impl fmt::Display for StringName {
352362
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
353363
let s = GString::from(self);

godot-core/src/obj/on_ready.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ use crate::registry::property::Var;
104104
/// impl INode for MyClass {
105105
/// fn ready(&mut self) {
106106
/// // self.node is now ready with the node found at path `ChildPath`.
107-
/// assert_eq!(self.auto.get_name(), "ChildPath".into());
107+
/// assert_eq!(self.auto.get_name(), "ChildPath");
108108
///
109109
/// // self.manual needs to be initialized manually.
110110
/// self.manual.init(22);

itest/rust/build.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ fn collect_inputs() -> Vec<Input> {
121121
push!(inputs; bool, bool, true);
122122
push!(inputs; Color, Color, Color(0.7, 0.5, 0.3, 0.2), Color::from_rgba(0.7, 0.5, 0.3, 0.2));
123123
push!(inputs; String, GString, "hello", GString::from("hello"));
124-
push!(inputs; StringName, StringName, &"hello", "hello".into());
125-
pushs!(inputs; NodePath, NodePath, r#"^"hello""#, "hello".into(), true, true, None);
124+
push!(inputs; StringName, StringName, &"hello", StringName::from("hello"));
125+
pushs!(inputs; NodePath, NodePath, r#"^"hello""#, NodePath::from("hello"), true, true, None);
126126
push!(inputs; Vector2, Vector2, Vector2(12.5, -3.5), Vector2::new(12.5, -3.5));
127127
push!(inputs; Vector3, Vector3, Vector3(117.5, 100.0, -323.25), Vector3::new(117.5, 100.0, -323.25));
128128
push!(inputs; Vector4, Vector4, Vector4(-18.5, 24.75, -1.25, 777.875), Vector4::new(-18.5, 24.75, -1.25, 777.875));
@@ -171,8 +171,8 @@ fn collect_inputs() -> Vec<Input> {
171171
push_newtype!(inputs; bool, NewBool(bool), true);
172172
push_newtype!(inputs; Color, NewColor(Color), Color(0.7, 0.5, 0.3, 0.2), NewColor(Color::from_rgba(0.7, 0.5, 0.3, 0.2)));
173173
push_newtype!(inputs; String, NewString(GString), "hello", NewString(GString::from("hello")));
174-
push_newtype!(inputs; StringName, NewStringName(StringName), &"hello", NewStringName("hello".into()));
175-
push_newtype!(@s inputs; NodePath, NewNodePath(NodePath), r#"^"hello""#, NewNodePath("hello".into()));
174+
push_newtype!(inputs; StringName, NewStringName(StringName), &"hello", NewStringName(StringName::from("hello")));
175+
push_newtype!(@s inputs; NodePath, NewNodePath(NodePath), r#"^"hello""#, NewNodePath(NodePath::from("hello")));
176176
push_newtype!(inputs; Vector2, NewVector2(Vector2), Vector2(12.5, -3.5), NewVector2(Vector2::new(12.5, -3.5)));
177177
push_newtype!(inputs; Vector3, NewVector3(Vector3), Vector3(117.5, 100.0, -323.25), NewVector3(Vector3::new(117.5, 100.0, -323.25)));
178178
push_newtype!(inputs; Vector4, NewVector4(Vector4), Vector4(-18.5, 24.75, -1.25, 777.875), NewVector4(Vector4::new(-18.5, 24.75, -1.25, 777.875)));

itest/rust/src/builtin_tests/containers/array_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,10 @@ func make_array() -> Array[CustomScriptForArrays]:
664664
let script = script.script().expect("script object should be alive");
665665
assert_eq!(script, gdscript.upcast());
666666
assert_eq!(script.get_name(), GString::new()); // Resource name.
667-
assert_eq!(script.get_instance_base_type(), "RefCounted".into());
667+
assert_eq!(script.get_instance_base_type(), "RefCounted");
668668

669669
#[cfg(since_api = "4.3")]
670-
assert_eq!(script.get_global_name(), "CustomScriptForArrays".into());
670+
assert_eq!(script.get_global_name(), "CustomScriptForArrays");
671671
}
672672

673673
// Test that proper type has been set&cached while creating new Array.

itest/rust/src/builtin_tests/containers/dictionary_test.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,5 @@ func variant_script_dict() -> Dictionary[Variant, CustomScriptForDictionaries]:
822822
assert_match!(dict.value_element_type(), ElementType::ScriptClass(script));
823823
let script = script.script().expect("script object should be alive");
824824
assert_eq!(script, gdscript.upcast());
825-
assert_eq!(
826-
script.get_global_name(),
827-
"CustomScriptForDictionaries".into()
828-
);
825+
assert_eq!(script.get_global_name(), "CustomScriptForDictionaries");
829826
}

itest/rust/src/builtin_tests/script/script_instance_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl ScriptInstance for TestScriptInstance {
138138
}
139139

140140
fn set_property(mut this: SiMut<Self>, name: StringName, value: &Variant) -> bool {
141-
if name.to_string() == "script_property_b" {
141+
if name == "script_property_b" {
142142
this.script_property_b = FromGodot::from_variant(value);
143143
true
144144
} else {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,23 @@ fn node_path_subpath() {
109109
#[itest]
110110
fn node_path_get_name() {
111111
let path = NodePath::from("../RigidBody2D/Sprite2D");
112-
assert_eq!(path.get_name(0), "..".into());
113-
assert_eq!(path.get_name(1), "RigidBody2D".into());
114-
assert_eq!(path.get_name(2), "Sprite2D".into());
112+
assert_eq!(path.get_name(0), "..");
113+
assert_eq!(path.get_name(1), "RigidBody2D");
114+
assert_eq!(path.get_name(2), "Sprite2D");
115115

116116
expect_panic_or_nothing("NodePath::get_name() out of bounds", || {
117-
assert_eq!(path.get_name(3), "".into());
117+
assert_eq!(path.get_name(3), "");
118118
})
119119
}
120120

121121
#[itest]
122122
fn node_path_get_subname() {
123123
let path = NodePath::from("Sprite2D:texture:resource_name");
124-
assert_eq!(path.get_subname(0), "texture".into());
125-
assert_eq!(path.get_subname(1), "resource_name".into());
124+
assert_eq!(path.get_subname(0), "texture");
125+
assert_eq!(path.get_subname(1), "resource_name");
126126

127127
expect_panic_or_nothing("NodePath::get_subname() out of bounds", || {
128-
assert_eq!(path.get_subname(2), "".into());
128+
assert_eq!(path.get_subname(2), "");
129129
})
130130
}
131131

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ fn string_name_equality() {
5151
assert_ne!(string, different);
5252
}
5353

54+
#[itest]
55+
fn string_name_eq_str() {
56+
let apple = StringName::from("apple");
57+
assert_eq!(apple, "apple");
58+
assert_ne!(apple, "orange");
59+
}
60+
5461
#[itest]
5562
#[allow(clippy::eq_op)]
5663
fn string_name_transient_ord() {

itest/rust/src/object_tests/dyn_gd_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ fn dyn_gd_call_godot_method() {
282282
let mut node = foreign::NodeHealth::new_alloc().into_dyn::<dyn Health>();
283283

284284
node.set_name("dyn-name!");
285-
assert_eq!(node.get_name(), "dyn-name!".into());
285+
assert_eq!(node.get_name(), "dyn-name!");
286286

287287
node.free();
288288
}

itest/rust/src/object_tests/object_arg_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ where
228228

229229
assert_eq!(a, global::Error::OK);
230230
assert_eq!(b, global::Error::OK);
231-
assert_eq!(manual2.get_name(), "hello".into());
231+
assert_eq!(manual2.get_name(), "hello");
232232
assert_eq!(refc2.bind().value, -123);
233233

234234
manual2.free();

0 commit comments

Comments
 (0)