Skip to content

Commit

Permalink
Auto-impls: always Ord instead of PartialOrd (can't fail), use twice …
Browse files Browse the repository at this point in the history
…op< instead of op== and op<
  • Loading branch information
Bromeon committed Jul 17, 2022
1 parent 6bdcfe4 commit e2ad37c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion gdnative-core/src/core_types/rid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ impl_basic_traits_as_sys! {
for Rid as godot_rid {
Default => godot_rid_new;
Eq => godot_rid_operator_equal;
PartialOrd => godot_rid_operator_less;
Ord => godot_rid_operator_less;
}
}
2 changes: 1 addition & 1 deletion gdnative-core/src/core_types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ impl_basic_traits_as_sys! {
for StringName as godot_string_name {
Drop => godot_string_name_destroy;
Eq => godot_string_name_operator_equal;
PartialOrd => godot_string_name_operator_less;
Ord => godot_string_name_operator_less;
}
}

Expand Down
2 changes: 1 addition & 1 deletion gdnative-core/src/core_types/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ impl_basic_traits_as_sys!(
Drop => godot_variant_destroy;
Clone => godot_variant_new_copy;
PartialEq => godot_variant_operator_equal;
PartialOrd => godot_variant_operator_less;
Ord => godot_variant_operator_less;
}
);

Expand Down
33 changes: 14 additions & 19 deletions gdnative-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,30 +174,25 @@ macro_rules! impl_basic_trait_as_sys {
};

(
PartialOrd for $Type:ty as $GdType:ident : $gd_method:ident
Ord for $Type:ty as $GdType:ident : $gd_method:ident
) => {
impl PartialOrd for $Type {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
if self == other {
Some(std::cmp::Ordering::Equal)
} else if unsafe { (get_api().$gd_method)(&self.0, &other.0) } {
Some(std::cmp::Ordering::Less)
} else {
Some(std::cmp::Ordering::Greater)
}
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
};

(
Ord for $Type:ty as $GdType:ident : $gd_method:ident
) => {
impl_basic_trait_as_sys!(PartialOrd for $Type as $GdType : $gd_method);
impl Ord for $Type {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).unwrap()
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let op_less = get_api().$gd_method;
if unsafe { op_less(&self.0, &other.0) } {
std::cmp::Ordering::Less
} else if unsafe { op_less(&other.0, &self.0) } {
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
}
}
}
};
Expand Down

0 comments on commit e2ad37c

Please sign in to comment.