about summary refs log tree commit diff
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2023-01-12 22:39:25 -0800
committerScott McMurray <scottmcm@users.noreply.github.com>2023-01-12 22:39:25 -0800
commitfcbc12eae35296841b0ddd3bacbb43e1d0ae654e (patch)
treeadc30e04c9b1b4c275815c8d2b3188b048ff778f
parentbfffe406fbcabb37b95779f2d252c4a277191e0d (diff)
downloadrust-fcbc12eae35296841b0ddd3bacbb43e1d0ae654e.tar.gz
rust-fcbc12eae35296841b0ddd3bacbb43e1d0ae654e.zip
Implement `signum` with `Ord`
-rw-r--r--library/core/src/num/int_macros.rs11
1 files changed, 6 insertions, 5 deletions
diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs
index 21518a3f551..ba89553f65a 100644
--- a/library/core/src/num/int_macros.rs
+++ b/library/core/src/num/int_macros.rs
@@ -2574,12 +2574,13 @@ macro_rules! int_impl {
         #[must_use = "this returns the result of the operation, \
                       without modifying the original"]
         #[inline(always)]
+        #[rustc_allow_const_fn_unstable(const_cmp)]
         pub const fn signum(self) -> Self {
-            match self {
-                n if n > 0 =>  1,
-                0          =>  0,
-                _          => -1,
-            }
+            // Picking the right way to phrase this is complicated
+            // (<https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign>)
+            // so delegate it to `Ord` which is already producing -1/0/+1
+            // exactly like we need and can be the place to deal with the complexity.
+            self.cmp(&0) as _
         }
 
         /// Returns `true` if `self` is positive and `false` if the number is zero or