Skip to content

Commit 5b5bef4

Browse files
ecobiubiuBromeon
andauthored
Merge #911
911: `Ord` implementation now covered by macro r=Bromeon a=Bromeon Unifies `impl Ord` for various types. Also upgrades from `PartialOrd` to `Ord`, since Godot defines total order on these types. Co-authored-by: Jan Haller <[email protected]>
2 parents 337790d + 88676ac commit 5b5bef4

File tree

4 files changed

+29
-55
lines changed

4 files changed

+29
-55
lines changed

gdnative-core/src/core_types/rid.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::private::get_api;
22
use crate::sys;
3-
use std::cmp::Ordering;
43
use std::mem::transmute;
54

65
// Note: for safety design, consult:
@@ -90,20 +89,8 @@ impl Rid {
9089

9190
impl_basic_traits_as_sys! {
9291
for Rid as godot_rid {
93-
Eq => godot_rid_operator_equal;
9492
Default => godot_rid_new;
95-
}
96-
}
97-
98-
impl PartialOrd for Rid {
99-
#[inline]
100-
fn partial_cmp(&self, other: &Rid) -> Option<Ordering> {
101-
if PartialEq::eq(self, other) {
102-
Some(Ordering::Equal)
103-
} else if unsafe { (get_api().godot_rid_operator_less)(self.sys(), other.sys()) } {
104-
Some(Ordering::Less)
105-
} else {
106-
Some(Ordering::Greater)
107-
}
93+
Eq => godot_rid_operator_equal;
94+
Ord => godot_rid_operator_less;
10895
}
10996
}

gdnative-core/src/core_types/string.rs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::object::NewRef;
33
use crate::private::get_api;
44
use crate::sys;
55

6-
use std::cmp::Ordering;
76
use std::ffi::CStr;
87
use std::fmt;
98
use std::mem::forget;
@@ -301,6 +300,7 @@ impl_basic_traits_as_sys!(
301300
for GodotString as godot_string {
302301
Drop => godot_string_destroy;
303302
Eq => godot_string_operator_equal;
303+
Ord => godot_string_operator_less;
304304
Default => godot_string_new;
305305
NewRef => godot_string_new_copy;
306306
}
@@ -382,26 +382,6 @@ where
382382
}
383383
}
384384

385-
impl PartialOrd for GodotString {
386-
#[inline]
387-
fn partial_cmp(&self, other: &GodotString) -> Option<Ordering> {
388-
Some(self.cmp(other))
389-
}
390-
}
391-
392-
impl Ord for GodotString {
393-
#[inline]
394-
fn cmp(&self, other: &GodotString) -> Ordering {
395-
if self == other {
396-
Ordering::Equal
397-
} else if unsafe { (get_api().godot_string_operator_less)(&self.0, &other.0) } {
398-
Ordering::Less
399-
} else {
400-
Ordering::Greater
401-
}
402-
}
403-
}
404-
405385
/// Type representing a character in Godot's native encoding. Can be converted to and
406386
/// from `char`. Depending on the platform, this might not always be able to represent
407387
/// a full code point.
@@ -612,6 +592,7 @@ impl_basic_traits_as_sys! {
612592
for StringName as godot_string_name {
613593
Drop => godot_string_name_destroy;
614594
Eq => godot_string_name_operator_equal;
595+
Ord => godot_string_name_operator_less;
615596
}
616597
}
617598

@@ -622,21 +603,6 @@ impl fmt::Debug for StringName {
622603
}
623604
}
624605

625-
impl PartialOrd for StringName {
626-
#[inline]
627-
fn partial_cmp(&self, other: &StringName) -> Option<Ordering> {
628-
unsafe {
629-
let native = (get_api().godot_string_name_operator_less)(&self.0, &other.0);
630-
631-
if native {
632-
Some(Ordering::Less)
633-
} else {
634-
Some(Ordering::Greater)
635-
}
636-
}
637-
}
638-
}
639-
640606
impl<S> From<S> for GodotString
641607
where
642608
S: AsRef<str>,
@@ -699,6 +665,7 @@ mod serialize {
699665

700666
godot_test!(test_string {
701667
use crate::core_types::{GodotString, Variant, VariantType, ToVariant, VariantArray, Dictionary};
668+
use std::cmp::Ordering;
702669

703670
let foo: GodotString = "foo".into();
704671
assert_eq!(foo.len(), 3);

gdnative-core/src/core_types/variant.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ impl_basic_traits_as_sys!(
603603
Drop => godot_variant_destroy;
604604
Clone => godot_variant_new_copy;
605605
PartialEq => godot_variant_operator_equal;
606+
Ord => godot_variant_operator_less;
606607
}
607608
);
608609

gdnative-core/src/macros.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,32 @@ macro_rules! impl_basic_trait_as_sys {
169169
(
170170
Eq for $Type:ty as $GdType:ident : $gd_method:ident
171171
) => {
172-
impl PartialEq for $Type {
172+
impl_basic_trait_as_sys!(PartialEq for $Type as $GdType : $gd_method);
173+
impl Eq for $Type {}
174+
};
175+
176+
(
177+
Ord for $Type:ty as $GdType:ident : $gd_method:ident
178+
) => {
179+
impl PartialOrd for $Type {
173180
#[inline]
174-
fn eq(&self, other: &Self) -> bool {
175-
unsafe { (get_api().$gd_method)(self.sys(), other.sys()) }
181+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
182+
Some(self.cmp(other))
183+
}
184+
}
185+
impl Ord for $Type {
186+
#[inline]
187+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
188+
let op_less = get_api().$gd_method;
189+
if unsafe { op_less(&self.0, &other.0) } {
190+
std::cmp::Ordering::Less
191+
} else if unsafe { op_less(&other.0, &self.0) } {
192+
std::cmp::Ordering::Greater
193+
} else {
194+
std::cmp::Ordering::Equal
195+
}
176196
}
177197
}
178-
impl Eq for $Type {}
179198
};
180199

181200
(

0 commit comments

Comments
 (0)