about summary refs log tree commit diff
path: root/library/core/src/tuple.rs
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2023-02-16 23:59:13 -0800
committerScott McMurray <scottmcm@users.noreply.github.com>2023-02-16 23:59:13 -0800
commit680e21687d5540a425f11d468edcda0ce39c6289 (patch)
tree835fb93e8a06c2a3a9a3045efdf32dfebff8c0c8 /library/core/src/tuple.rs
parentdc37e373292d00572ff884a1a095cc4574d9fe82 (diff)
downloadrust-680e21687d5540a425f11d468edcda0ce39c6289.tar.gz
rust-680e21687d5540a425f11d468edcda0ce39c6289.zip
Use `partial_cmp` to implement tuple `lt`/`le`/`ge`/`gt`
Diffstat (limited to 'library/core/src/tuple.rs')
-rw-r--r--library/core/src/tuple.rs48
1 files changed, 35 insertions, 13 deletions
diff --git a/library/core/src/tuple.rs b/library/core/src/tuple.rs
index 28275798f75..0620e7173bc 100644
--- a/library/core/src/tuple.rs
+++ b/library/core/src/tuple.rs
@@ -1,7 +1,7 @@
 // See src/libstd/primitive_docs.rs for documentation.
 
-use crate::cmp::Ordering::*;
-use crate::cmp::*;
+use crate::cmp::Ordering::{self, *};
+use crate::mem::transmute;
 
 // Recursive macro for implementing n-ary tuple functions and operations
 //
@@ -61,19 +61,19 @@ macro_rules! tuple_impls {
                 }
                 #[inline]
                 fn lt(&self, other: &($($T,)+)) -> bool {
-                    lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
+                    lexical_ord!(lt, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
                 }
                 #[inline]
                 fn le(&self, other: &($($T,)+)) -> bool {
-                    lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
+                    lexical_ord!(le, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
                 }
                 #[inline]
                 fn ge(&self, other: &($($T,)+)) -> bool {
-                    lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
+                    lexical_ord!(ge, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
                 }
                 #[inline]
                 fn gt(&self, other: &($($T,)+)) -> bool {
-                    lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
+                    lexical_ord!(gt, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
                 }
             }
         }
@@ -123,16 +123,38 @@ macro_rules! maybe_tuple_doc {
     };
 }
 
-// Constructs an expression that performs a lexical ordering using method $rel.
+#[inline]
+const fn ordering_is_some(c: Option<Ordering>, x: Ordering) -> bool {
+    // FIXME: Just use `==` once that's const-stable on `Option`s.
+    // This isn't using `match` because that optimizes worse due to
+    // making a two-step check (`Some` *then* the inner value).
+
+    // SAFETY: There's no public guarantee for `Option<Ordering>`,
+    // but we're core so we know that it's definitely a byte.
+    unsafe {
+        let c: i8 = transmute(c);
+        let x: i8 = transmute(Some(x));
+        c == x
+    }
+}
+
+// Constructs an expression that performs a lexical ordering using method `$rel`.
 // The values are interleaved, so the macro invocation for
-// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
-// a3, b3)` (and similarly for `lexical_cmp`)
+// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, opt_is_lt, a1, b1,
+// a2, b2, a3, b3)` (and similarly for `lexical_cmp`)
+//
+// `$ne_rel` is only used to determine the result after checking that they're
+// not equal, so `lt` and `le` can both just use `Less`.
 macro_rules! lexical_ord {
-    ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
-        if $a != $b { lexical_ord!($rel, $a, $b) }
-        else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
+    ($rel: ident, $ne_rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {{
+        let c = PartialOrd::partial_cmp(&$a, &$b);
+        if !ordering_is_some(c, Equal) { ordering_is_some(c, $ne_rel) }
+        else { lexical_ord!($rel, $ne_rel, $($rest_a, $rest_b),+) }
+    }};
+    ($rel: ident, $ne_rel: ident, $a:expr, $b:expr) => {
+        // Use the specific method for the last element
+        PartialOrd::$rel(&$a, &$b)
     };
-    ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
 }
 
 macro_rules! lexical_partial_cmp {