tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 583HTML 275Markdown 8TOML 41586 Sutichaykuna

Drop in support for no_std float operations

12ef2852bdb59d1d6ed1a785de4a11a4fb6ba017 · ruwaq David Flemström · (6 wata ñawpaq) · tayta 619b2b16a · kay commitpi sach'ata qhaway

Semántico sayk'uy · 11

src/algo.rs

src/id.rs

  • silq'uyninpi t'ikrakurqan def Allocator
  • silq'uyninpi t'ikrakurqan def Id
  • ukhunpi t'ikrakurqan fn new pub

src/sys.rs

4 qillqakuna · +41 −10

diff --git a/Cargo.toml b/Cargo.toml
index 4bbc9cf9c..203db66b4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,6 +20,7 @@ arrayvec = { version = "0.5.1", default-features = false }
 hash32 = "0.1.1"
 hash32-derive = "0.1.0"
 heapless = "0.5.1"
+num-traits = { version = "0.2.10", default-features = false }
 typenum = "1.11.2"
 
 [dependencies.hashbrown]
@@ -34,7 +35,7 @@ optional = true
 [features]
 default = ["std"]
 alloc = ["hashbrown"]
-std = []
+std = ["num-traits/std"]
 serde_camel_case = ["serde"]
 serde_kebab_case = ["serde"]
 
diff --git a/src/algo.rs b/src/algo.rs
index 42457a780..df9b834a4 100644
--- a/src/algo.rs
+++ b/src/algo.rs
@@ -118,10 +118,10 @@ impl Forest {
         let abs_x = abs_x + layout.location.x;
         let abs_y = abs_y + layout.location.y;
 
-        layout.location.x = layout.location.x.round();
-        layout.location.y = layout.location.y.round();
-        layout.size.width = (abs_x + layout.size.width).round() - abs_x.round();
-        layout.size.height = (abs_y + layout.size.height).round() - abs_y.round();
+        layout.location.x = sys::round(layout.location.x);
+        layout.location.y = sys::round(layout.location.y);
+        layout.size.width = sys::round(abs_x + layout.size.width) - sys::round(abs_x);
+        layout.size.height = sys::round(abs_y + layout.size.height) - sys::round(abs_y);
         for child in &children[root] {
             Self::round_layout(nodes, children, *child, abs_x, abs_y);
         }
@@ -141,13 +141,13 @@ impl Forest {
         if let Some(ref cache) = self.nodes[node].layout_cache {
             if cache.perform_layout || !perform_layout {
                 let width_compatible = if let Number::Defined(width) = node_size.width {
-                    (width - cache.result.size.width).abs() < f32::EPSILON
+                    sys::abs(width - cache.result.size.width) < f32::EPSILON
                 } else {
                     cache.node_size.width.is_undefined()
                 };
 
                 let height_compatible = if let Number::Defined(height) = node_size.height {
-                    (height - cache.result.size.height).abs() < f32::EPSILON
+                    sys::abs(height - cache.result.size.height) < f32::EPSILON
                 } else {
                     cache.node_size.height.is_undefined()
                 };
diff --git a/src/id.rs b/src/id.rs
index 4e2586312..7da241348 100644
--- a/src/id.rs
+++ b/src/id.rs
@@ -8,15 +8,15 @@ pub(crate) type NodeId = usize;
 
 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
 #[cfg_attr(not(any(feature = "std", feature = "alloc")), derive(hash32_derive::Hash32))]
-pub(crate) struct Id(u64);
+pub(crate) struct Id(usize);
 
 pub(crate) struct Allocator {
-    new_id: atomic::AtomicU64,
+    new_id: atomic::AtomicUsize,
 }
 
 impl Allocator {
     pub const fn new() -> Self {
-        Allocator { new_id: atomic::AtomicU64::new(0) }
+        Allocator { new_id: atomic::AtomicUsize::new(0) }
     }
 
     pub fn allocate(&self) -> Id {
diff --git a/src/sys.rs b/src/sys.rs
index 51c59291d..acaf20ce1 100644
--- a/src/sys.rs
+++ b/src/sys.rs
@@ -16,6 +16,16 @@ mod std {
     pub fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
         Vec::with_capacity(capacity)
     }
+
+    #[inline]
+    pub fn round(value: f32) -> f32 {
+        value.round()
+    }
+
+    #[inline]
+    pub fn abs(value: f32) -> f32 {
+        value.abs()
+    }
 }
 
 #[cfg(feature = "alloc")]
@@ -33,6 +43,16 @@ mod alloc {
     pub fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
         Vec::with_capacity(capacity)
     }
+
+    #[inline]
+    pub fn round(value: f32) -> f32 {
+        num_traits::float::FloatCore::round(value)
+    }
+
+    #[inline]
+    pub fn abs(value: f32) -> f32 {
+        num_traits::float::FloatCore::abs(value)
+    }
 }
 
 #[cfg(all(not(feature = "alloc"), not(feature = "std")))]
@@ -61,6 +81,16 @@ mod core {
     {
         ::arrayvec::ArrayVec::new()
     }
+
+    #[inline]
+    pub fn round(value: f32) -> f32 {
+        num_traits::float::FloatCore::round(value)
+    }
+
+    #[inline]
+    pub fn abs(value: f32) -> f32 {
+        num_traits::float::FloatCore::abs(value)
+    }
 }
 
 #[cfg(feature = "alloc")]