tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 238Rust 13TOML 3CSS 1146 Sutichaykuna

Implement core/alloc compatible versions of f32_max and f32_min

397e3158fc99c64fab0f651260f8663207645943 · ruwaq Nico Burns · (3 wata ñawpaq) · tayta 60840bb43 · kay commitpi sach'ata qhaway

Semántico sayk'uy · 6

src/sys.rs

1 qillqakuna · +48 −11

diff --git a/src/sys.rs b/src/sys.rs
index de8fb05ca..40c01f42c 100644
--- a/src/sys.rs
+++ b/src/sys.rs
@@ -12,17 +12,6 @@ pub(crate) use self::alloc::*;
 #[cfg(all(not(feature = "alloc"), not(feature = "std")))]
 pub(crate) use self::core::*;
 
-/// Returns the largest of two f32 values
-pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
-    core::cmp::max_by(a, b, |a, b| a.total_cmp(b))
-}
-
-/// Returns the smallest of two f32 values
-#[cfg(feature = "grid")]
-pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
-    core::cmp::min_by(a, b, |a, b| a.total_cmp(b))
-}
-
 /// For when `std` is enabled
 #[cfg(feature = "std")]
 mod std {
@@ -53,12 +42,24 @@ mod std {
     pub(crate) fn abs(value: f32) -> f32 {
         value.abs()
     }
+
+    /// Returns the largest of two f32 values
+    pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
+        std::cmp::max_by(a, b, |a, b| a.total_cmp(&b))
+    }
+
+    /// Returns the smallest of two f32 values
+    #[cfg(feature = "grid")]
+    pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
+        std::cmp::min_by(a, b, |a, b| a.total_cmp(&b))
+    }
 }
 
 /// For when `alloc` but not `std` is enabled
 #[cfg(all(feature = "alloc", not(feature = "std")))]
 mod alloc {
     extern crate alloc;
+    use core::cmp::Ordering;
 
     /// An allocation-backend agnostic `Box` type
     pub(crate) type Box<A> = alloc::boxed::Box<A>;
@@ -86,11 +87,30 @@ mod alloc {
     pub(crate) fn abs(value: f32) -> f32 {
         num_traits::float::FloatCore::abs(value)
     }
+
+    /// Returns the largest of two f32 values
+    pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
+        match a.total_cmp(&b) {
+            Ordering::Greater | Ordering::Equal => a,
+            Ordering::Less => b,
+        }
+    }
+
+    /// Returns the smallest of two f32 values
+    #[cfg(feature = "grid")]
+    pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
+        match a.total_cmp(&b) {
+            Ordering::Less => a,
+            Ordering::Greater | Ordering::Equal => b,
+        }
+    }
 }
 
 /// For when neither `alloc` nor `std` is enabled
 #[cfg(all(not(feature = "alloc"), not(feature = "std")))]
 mod core {
+    use core::cmp::Ordering;
+
     /// The maximum number of nodes in the tree
     pub const MAX_NODE_COUNT: usize = 256;
     /// The maximum number of children of any given node
@@ -126,4 +146,21 @@ mod core {
     pub(crate) fn abs(value: f32) -> f32 {
         num_traits::float::FloatCore::abs(value)
     }
+
+    /// Returns the largest of two f32 values
+    pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
+        match a.total_cmp(&b) {
+            Ordering::Greater | Ordering::Equal => a,
+            Ordering::Less => b,
+        }
+    }
+
+    /// Returns the smallest of two f32 values
+    #[cfg(feature = "grid")]
+    pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
+        match a.total_cmp(&b) {
+            Ordering::Less => a,
+            Ordering::Greater | Ordering::Equal => b,
+        }
+    }
 }