tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 583HTML 275Markdown 8TOML 41586 Definitions

Fully remove num-traits (#762)

Signed-off-by: Nico Burns <nico@nicoburns.com>

0120d254ada2bc499f5a3b39923ad1bf368667c1 · by Nico Burns · (1 year ago) · parent 016b93a3d · browse the tree at this commit

Semantic delta · 5

src/compute/grid/explicit_grid.rs

src/util/sys.rs

3 files · +52 −11

diff --git a/Cargo.toml b/Cargo.toml
index 568b58727..4856e9d30 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,7 +18,6 @@ license = "MIT"
 [dependencies]
 arrayvec = { version = "0.7", default-features = false }
 document-features = { version = "0.2.7", optional = true }
-num-traits = { version = "0.2", default-features = false }
 serde = { version = "1.0", default-features = false, optional = true, features = [
     "serde_derive",
 ] }
@@ -66,7 +65,7 @@ taffy_tree = ["dep:slotmap"]
 ## Add [`serde`] derives to Style structs
 serde = ["dep:serde"]
 ## Allow Taffy to depend on the [`Rust Standard Library`](std)
-std = ["num-traits/std", "grid?/std", "serde?/std", "slotmap?/std"]
+std = ["grid?/std", "serde?/std", "slotmap?/std"]
 ## Allow Taffy to depend on the alloc library
 alloc = ["serde?/alloc"]
 ## Internal feature for debugging
diff --git a/src/compute/grid/explicit_grid.rs b/src/compute/grid/explicit_grid.rs
index d292fe676..ce862f286 100644
--- a/src/compute/grid/explicit_grid.rs
+++ b/src/compute/grid/explicit_grid.rs
@@ -4,14 +4,11 @@ use super::types::{GridTrack, TrackCounts};
 use crate::geometry::{AbsoluteAxis, Size};
 use crate::style::{GridTrackRepetition, LengthPercentage, NonRepeatedTrackSizingFunction, TrackSizingFunction};
 use crate::style_helpers::TaffyAuto;
-use crate::util::sys::Vec;
+use crate::util::sys::{ceil, floor, Vec};
 use crate::util::MaybeMath;
 use crate::util::ResolveOrZero;
 use crate::{GridContainerStyle, MaybeResolve};
 
-#[cfg(not(feature = "std"))]
-use num_traits::float::FloatCore;
-
 /// Compute the number of rows and columns in the explicit grid
 pub(crate) fn compute_explicit_grid_size_in_axis(
     style: &impl GridContainerStyle,
@@ -161,9 +158,9 @@ pub(crate) fn compute_explicit_grid_size_in_axis(
                 //
                 // In all cases we add the additional repetition that was already accounted for in the special-case computation above
                 if size_is_maximum {
-                    (num_repetition_that_fit.floor() as u16) + 1
+                    (floor(num_repetition_that_fit) as u16) + 1
                 } else {
-                    (num_repetition_that_fit.ceil() as u16) + 1
+                    (ceil(num_repetition_that_fit) as u16) + 1
                 }
             }
         }
diff --git a/src/util/sys.rs b/src/util/sys.rs
index ecf299e76..8e1d8d368 100644
--- a/src/util/sys.rs
+++ b/src/util/sys.rs
@@ -36,6 +36,20 @@ mod std {
         value.round()
     }
 
+    /// Rounds up to the nearest whole number
+    #[must_use]
+    #[inline(always)]
+    pub(crate) fn ceil(value: f32) -> f32 {
+        value.ceil()
+    }
+
+    /// Rounds down to the nearest whole number
+    #[must_use]
+    #[inline(always)]
+    pub(crate) fn floor(value: f32) -> f32 {
+        value.floor()
+    }
+
     /// Computes the absolute value
     #[must_use]
     #[inline(always)]
@@ -79,6 +93,12 @@ mod alloc {
     /// Rounds to the nearest whole number
     pub(crate) use super::polyfill::round;
 
+    /// Rounds up to the nearest whole number
+    pub(crate) use super::polyfill::ceil;
+
+    /// Rounds down to the nearest whole number
+    pub(crate) use super::polyfill::floor;
+
     /// Computes the absolute value
     pub(crate) use super::polyfill::abs;
 
@@ -145,9 +165,10 @@ mod core {
 
 /// Implementations of float functions for no_std and alloc builds
 /// Copied from `num-traits` crate
+#[cfg(not(feature = "std"))]
 mod polyfill {
+    #[must_use]
     #[inline(always)]
-    #[cfg(not(feature = "std"))]
     fn fract(value: f32) -> f32 {
         if value == 0.0 {
             0.0
@@ -158,7 +179,6 @@ mod polyfill {
 
     #[must_use]
     #[inline(always)]
-    #[cfg(not(feature = "std"))]
     pub(crate) fn round(value: f32) -> f32 {
         let f = fract(value);
         if f.is_nan() || f == 0.0 {
@@ -176,10 +196,35 @@ mod polyfill {
         }
     }
 
+    #[must_use]
+    #[inline(always)]
+    pub(crate) fn floor(value: f32) -> f32 {
+        let f = fract(value);
+        if f.is_nan() || f == 0.0 {
+            value
+        } else if value < 0.0 {
+            value - f - 1.0
+        } else {
+            value - f
+        }
+    }
+
+    #[must_use]
+    #[inline(always)]
+    pub(crate) fn ceil(value: f32) -> f32 {
+        let f = fract(value);
+        if f.is_nan() || f == 0.0 {
+            value
+        } else if value > 0.0 {
+            value - f + 1.0
+        } else {
+            value - f
+        }
+    }
+
     /// Computes the absolute value
     #[must_use]
     #[inline(always)]
-    #[cfg(not(feature = "std"))]
     pub(crate) fn abs(value: f32) -> f32 {
         if value.is_sign_positive() {
             return value;