@@ -2,6 +2,7 @@ use crate::core_types::Variant;
22use crate :: object:: NewRef ;
33use crate :: private:: get_api;
44use crate :: sys;
5+ use std:: cmp:: Ordering ;
56
67use std:: ffi:: CStr ;
78use std:: fmt;
@@ -591,8 +592,36 @@ impl StringName {
591592impl_basic_traits_as_sys ! {
592593 for StringName as godot_string_name {
593594 Drop => godot_string_name_destroy;
594- Eq => godot_string_name_operator_equal;
595- Ord => godot_string_name_operator_less;
595+
596+ // Note: Godot's equal/less implementations contained a bug until Godot 3.5, see https://github.com/godot-rust/godot-rust/pull/912
597+ // Thus explicit impl as a workaround for now
598+ // Eq => godot_string_name_operator_equal;
599+ // Ord => godot_string_name_operator_less;
600+ }
601+ }
602+
603+ impl PartialEq for StringName {
604+ #[ inline]
605+ fn eq ( & self , other : & Self ) -> bool {
606+ // Slow but correct -- see comment above
607+ self . to_godot_string ( ) == other. to_godot_string ( )
608+ }
609+ }
610+
611+ impl Eq for StringName { }
612+
613+ impl PartialOrd for StringName {
614+ #[ inline]
615+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
616+ Some ( Ord :: cmp ( self , other) )
617+ }
618+ }
619+
620+ impl Ord for StringName {
621+ #[ inline]
622+ fn cmp ( & self , other : & Self ) -> Ordering {
623+ // Slow but correct -- see comment above
624+ Ord :: cmp ( & self . to_godot_string ( ) , & other. to_godot_string ( ) )
596625 }
597626}
598627
@@ -728,3 +757,47 @@ godot_test!(test_string {
728757 assert_eq!( fmt2, GodotString :: from( "foo bar" ) ) ;
729758 assert_eq!( fmt_string2, GodotString :: from( "{0} {1}" ) ) ;
730759} ) ;
760+
761+ godot_test ! ( test_string_name_eq {
762+ use crate :: core_types:: { GodotString , StringName } ;
763+
764+ let a: StringName = StringName :: from_str( "some string" ) ;
765+ let b: StringName = StringName :: from_godot_string( & GodotString :: from( "some string" ) ) ;
766+ let c: StringName = StringName :: from_str( String :: from( "some other string" ) ) ;
767+ let d: StringName = StringName :: from_str( "yet another one" ) ;
768+
769+ // test Eq
770+ assert_eq!( a, b) ;
771+ assert_ne!( a, c) ;
772+ assert_ne!( a, d) ;
773+ assert_ne!( b, c) ;
774+ assert_ne!( b, d) ;
775+ assert_ne!( c, d) ;
776+
777+ let back = b. to_godot_string( ) ;
778+ assert_eq!( back, GodotString :: from( "some string" ) ) ;
779+ } ) ;
780+
781+ godot_test ! ( test_string_name_ord {
782+ use crate :: core_types:: { GodotString , StringName } ;
783+
784+ let a: StringName = StringName :: from_str( "some string" ) ;
785+ let b: StringName = StringName :: from_godot_string( & GodotString :: from( "some string" ) ) ;
786+ let c: StringName = StringName :: from_str( String :: from( "some other string" ) ) ;
787+
788+ // test Ord
789+ let a_b = Ord :: cmp( & a, & b) ;
790+ let b_a = Ord :: cmp( & b, & a) ;
791+ assert_eq!( a_b, Ordering :: Equal ) ;
792+ assert_eq!( b_a, Ordering :: Equal ) ;
793+
794+ let a_c = Ord :: cmp( & a, & c) ;
795+ let c_a = Ord :: cmp( & c, & a) ;
796+ let b_c = Ord :: cmp( & b, & c) ;
797+ let c_b = Ord :: cmp( & c, & b) ;
798+ assert_ne!( a_c, Ordering :: Equal ) ;
799+ assert_eq!( a_c, b_c) ;
800+ assert_eq!( c_a, c_b) ;
801+ assert_eq!( a_c, c_a. reverse( ) ) ;
802+ assert_eq!( b_c, c_b. reverse( ) ) ;
803+ } ) ;
0 commit comments