gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 227Rust 7TOML 3CSS 1307 Sutichaykuna

Remove `ComputeResult` type (#139)

* Remove ComputeResult type; just use a Size<f32>

* Use more helpful variable names than `result`

* Thanks clippy

2366d417a73b1ac6e2b2dca660fec564549c2e0e · ruwaq Alice Cecile · (4 wata ñawpaq) · tayta 26191c9da · kay commitpi sach'ata qhaway

Semántico sayk'uy · 11

src/flexbox.rs

src/layout.rs

  • silq'uyninpi t'ikrakurqan def Cache

2 qillqakuna · +38 −57

diff --git a/src/flexbox.rs b/src/flexbox.rs
index dd61188ce..c579547d2 100644
--- a/src/flexbox.rs
+++ b/src/flexbox.rs
@@ -14,13 +14,6 @@ use crate::style::{AlignContent, AlignSelf, Dimension, Display, FlexWrap, Justif
 use crate::style::{FlexDirection, FlexboxLayout};
 use crate::sys::{abs, round, ChildrenVec, Vec};
 
-/// The computed results of a [`flexbox`](crate::flexbox) calculation
-#[derive(Debug, Clone)]
-pub(crate) struct ComputeResult {
-    /// The computed size
-    pub(crate) size: Size<f32>,
-}
-
 /// The intermediate results of a flexbox calculation for a single item
 struct FlexItem {
     /// The identifier for the associated [`Node`](crate::node::Node)
@@ -119,20 +112,18 @@ impl Forest {
             || style.max_size.width.is_defined()
             || style.max_size.height.is_defined();
 
-        let result = if has_root_min_max {
+        let preliminary_size = if has_root_min_max {
             let first_pass = self.compute_preliminary(root, style.size.resolve(size), size, false, true);
 
             self.compute_preliminary(
                 root,
                 Size {
                     width: first_pass
-                        .size
                         .width
                         .maybe_max(style.min_size.width.resolve(size.width))
                         .maybe_min(style.max_size.width.resolve(size.width))
                         .into(),
                     height: first_pass
-                        .size
                         .height
                         .maybe_max(style.min_size.height.resolve(size.height))
                         .maybe_min(style.max_size.height.resolve(size.height))
@@ -146,7 +137,7 @@ impl Forest {
             self.compute_preliminary(root, style.size.resolve(size), size, true, true)
         };
 
-        self.nodes[root].layout = Layout { order: 0, size: result.size, location: Point::zero() };
+        self.nodes[root].layout = Layout { order: 0, size: preliminary_size, location: Point::zero() };
 
         Self::round_layout(&mut self.nodes, &self.children, root, 0.0, 0.0);
     }
@@ -186,27 +177,27 @@ impl Forest {
         parent_size: Size<Number>,
         perform_layout: bool,
         main_size: bool,
-    ) -> Option<ComputeResult> {
+    ) -> Option<Size<f32>> {
         if let Some(ref cache) = self.cache(node, main_size) {
             if cache.perform_layout || !perform_layout {
                 let width_compatible = if let Number::Defined(width) = node_size.width {
-                    abs(width - cache.result.size.width) < f32::EPSILON
+                    abs(width - cache.size.width) < f32::EPSILON
                 } else {
                     cache.node_size.width.is_undefined()
                 };
 
                 let height_compatible = if let Number::Defined(height) = node_size.height {
-                    abs(height - cache.result.size.height) < f32::EPSILON
+                    abs(height - cache.size.height) < f32::EPSILON
                 } else {
                     cache.node_size.height.is_undefined()
                 };
 
                 if width_compatible && height_compatible {
-                    return Some(cache.result.clone());
+                    return Some(cache.size);
                 }
 
                 if cache.node_size == node_size && cache.parent_size == parent_size {
-                    return Some(cache.result.clone());
+                    return Some(cache.size);
                 }
             }
         }
@@ -434,7 +425,6 @@ impl Forest {
                     false,
                     true,
                 )
-                .size
                 .main(constants.dir)
                 .maybe_max(child.min_size.main(constants.dir))
                 .maybe_min(child.max_size.main(constants.dir));
@@ -454,7 +444,6 @@ impl Forest {
             // min-content max-content constraints from the top
             let min_main = self
                 .compute_preliminary(child.node, Size::undefined(), available_space, false, false)
-                .size
                 .main(constants.dir)
                 .maybe_max(child.min_size.main(constants.dir))
                 .maybe_min(child.size.main(constants.dir))
@@ -572,7 +561,6 @@ impl Forest {
                         false,
                         false,
                     )
-                    .size
                     .main(constants.dir)
                     .maybe_max(child.min_size.main(constants.dir))
                     .maybe_min(child.max_size.main(constants.dir)),
@@ -715,7 +703,6 @@ impl Forest {
                 // just piling on more conditionals.
                 let min_main = if constants.is_row && self.nodes[child.node].measure.is_none() {
                     self.compute_preliminary(child.node, Size::undefined(), available_space, false, false)
-                        .size
                         .width
                         .maybe_min(child.size.width)
                         .maybe_max(child.min_size.width)
@@ -800,7 +787,6 @@ impl Forest {
                     false,
                     false,
                 )
-                .size
                 .cross(constants.dir)
                 .maybe_max(child.min_size.cross(constants.dir))
                 .maybe_min(child.max_size.cross(constants.dir)),
@@ -834,7 +820,7 @@ impl Forest {
 
         for line in flex_lines {
             for child in line.items.iter_mut() {
-                let result = self.compute_preliminary(
+                let preliminary_size = self.compute_preliminary(
                     child.node,
                     Size {
                         width: if constants.is_row {
@@ -865,7 +851,7 @@ impl Forest {
                     child.node,
                     &Layout {
                         order: self.children[node].iter().position(|n| *n == child.node).unwrap() as u32,
-                        size: result.size,
+                        size: preliminary_size,
                         location: Point::zero(),
                     },
                 );
@@ -1346,7 +1332,7 @@ impl Forest {
             let line_offset_cross = line.offset_cross;
 
             let layout_item = |child: &mut FlexItem| {
-                let result = self.compute_preliminary(
+                let preliminary_size = self.compute_preliminary(
                     child.node,
                     child.target_size.map(|s| s.into()),
                     constants.container_size.map(|s| s.into()),
@@ -1369,15 +1355,16 @@ impl Forest {
 
                 self.nodes[child.node].layout = Layout {
                     order: self.children[node].iter().position(|n| *n == child.node).unwrap() as u32,
-                    size: result.size,
+                    size: preliminary_size,
                     location: Point {
                         x: if constants.is_row { offset_main } else { offset_cross },
                         y: if constants.is_column { offset_main } else { offset_cross },
                     },
                 };
 
-                total_offset_main +=
-                    child.offset_main + child.margin.main_axis_sum(constants.dir) + result.size.main(constants.dir);
+                total_offset_main += child.offset_main
+                    + child.margin.main_axis_sum(constants.dir)
+                    + preliminary_size.main(constants.dir);
             };
 
             if constants.dir.is_reverse() {
@@ -1449,7 +1436,7 @@ impl Forest {
                     Undefined
                 });
 
-            let result = self.compute_preliminary(
+            let preliminary_size = self.compute_preliminary(
                 child,
                 Size { width, height },
                 Size { width: container_width, height: container_height },
@@ -1458,8 +1445,7 @@ impl Forest {
             );
 
             let free_main_space = constants.container_size.main(constants.dir)
-                - result
-                    .size
+                - preliminary_size
                     .main(constants.dir)
                     .maybe_max(
                         child_style.min_main_size(constants.dir).resolve(constants.node_inner_size.main(constants.dir)),
@@ -1469,8 +1455,7 @@ impl Forest {
                     );
 
             let free_cross_space = constants.container_size.cross(constants.dir)
-                - result
-                    .size
+                - preliminary_size
                     .cross(constants.dir)
                     .maybe_max(
                         child_style
@@ -1534,7 +1519,7 @@ impl Forest {
 
             self.nodes[child].layout = Layout {
                 order: order as u32,
-                size: result.size,
+                size: preliminary_size,
                 location: Point {
                     x: if constants.is_row { offset_main } else { offset_cross },
                     y: if constants.is_column { offset_main } else { offset_cross },
@@ -1551,12 +1536,12 @@ impl Forest {
         parent_size: Size<Number>,
         perform_layout: bool,
         main_size: bool,
-    ) -> ComputeResult {
+    ) -> Size<f32> {
         self.nodes[node].is_dirty = false;
 
         // First we check if we have a result for the given input
-        if let Some(result) = self.compute_from_cache(node, node_size, parent_size, perform_layout, main_size) {
-            return result;
+        if let Some(cached_size) = self.compute_from_cache(node, node_size, parent_size, perform_layout, main_size) {
+            return cached_size;
         }
 
         // Define some general constants we will need for the remainder of the algorithm.
@@ -1565,25 +1550,23 @@ impl Forest {
         // If this is a leaf node we can skip a lot of this function in some cases
         if self.children[node].is_empty() {
             if node_size.width.is_defined() && node_size.height.is_defined() {
-                return ComputeResult { size: node_size.map(|s| s.or_else(0.0)) };
+                return node_size.map(|s| s.or_else(0.0));
             }
 
             if let Some(ref measure) = self.nodes[node].measure {
-                let result = match measure {
-                    MeasureFunc::Raw(measure) => ComputeResult { size: measure(node_size) },
+                let converted_size = match measure {
+                    MeasureFunc::Raw(measure) => measure(node_size),
                     #[cfg(any(feature = "std", feature = "alloc"))]
-                    MeasureFunc::Boxed(measure) => ComputeResult { size: measure(node_size) },
+                    MeasureFunc::Boxed(measure) => measure(node_size),
                 };
                 *self.cache(node, main_size) =
-                    Some(Cache { node_size, parent_size, perform_layout, result: result.clone() });
-                return result;
+                    Some(Cache { node_size, parent_size, perform_layout, size: converted_size });
+                return converted_size;
             }
 
-            return ComputeResult {
-                size: Size {
-                    width: node_size.width.or_else(0.0) + constants.padding_border.horizontal_axis_sum(),
-                    height: node_size.height.or_else(0.0) + constants.padding_border.vertical_axis_sum(),
-                },
+            return Size {
+                width: node_size.width.or_else(0.0) + constants.padding_border.horizontal_axis_sum(),
+                height: node_size.height.or_else(0.0) + constants.padding_border.vertical_axis_sum(),
             };
         }
 
@@ -1693,10 +1676,9 @@ impl Forest {
         // We have the container size.
         // If our caller does not care about performing layout we are done now.
         if !perform_layout {
-            let result = ComputeResult { size: constants.container_size };
-            *self.cache(node, main_size) =
-                Some(Cache { node_size, parent_size, perform_layout, result: result.clone() });
-            return result;
+            let container_size = constants.container_size;
+            *self.cache(node, main_size) = Some(Cache { node_size, parent_size, perform_layout, size: container_size });
+            return container_size;
         }
 
         // 16. Align all flex lines per align-content.
@@ -1725,9 +1707,9 @@ impl Forest {
             }
         }
 
-        let result = ComputeResult { size: constants.container_size };
-        *self.cache(node, main_size) = Some(Cache { node_size, parent_size, perform_layout, result: result.clone() });
+        let container_size = constants.container_size;
+        *self.cache(node, main_size) = Some(Cache { node_size, parent_size, perform_layout, size: container_size });
 
-        result
+        container_size
     }
 }
diff --git a/src/layout.rs b/src/layout.rs
index b09b859f4..dd465f7ff 100644
--- a/src/layout.rs
+++ b/src/layout.rs
@@ -1,6 +1,5 @@
 //! Final and cached data structures that represent the high-level UI layout
 
-use crate::flexbox::ComputeResult;
 use crate::geometry::{Point, Size};
 use crate::number::Number;
 
@@ -36,6 +35,6 @@ pub(crate) struct Cache {
     /// Whether or not layout should be recomputed
     pub(crate) perform_layout: bool,
 
-    /// The stored result of the layout calculations
-    pub(crate) result: ComputeResult,
+    /// The cached size of the item
+    pub(crate) size: Size<f32>,
 }