gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1112HTML 1039Markdown 12TOML 94628 Definitions

Merge `taffy_tree` module into a single file (#574)

* Simplify how dispatch function calls leaf layout algorithm

* Merge taffy_tree module into one file

278f5e39b6edcd6f5a3ee9e10da81c0bfb0b3124 · by Nico Burns · (2 years ago) · parent c16e2b683 · browse the tree at this commit

Semantic delta · 167

src/tree/taffy_tree.rs

src/tree/taffy_tree/error.rs

src/tree/taffy_tree/mod.rs

src/tree/taffy_tree/tree.rs

4 files · +990 −1018

diff --git a/src/tree/taffy_tree/tree.rs b/src/tree/taffy_tree.rs
similarity index 93%
rename from src/tree/taffy_tree/tree.rs
rename to src/tree/taffy_tree.rs
index 85f83e7c7..412305cc8 100644
--- a/src/tree/taffy_tree/tree.rs
+++ b/src/tree/taffy_tree.rs
@@ -1,6 +1,4 @@
-//! UI node types and related data structures.
-//!
-//! Layouts are composed of multiple nodes, which live in a tree-like data structure.
+//! Contains [Taffy](crate::tree::Taffy): the default implementation of [LayoutTree](crate::tree::LayoutTree), and the error type for Taffy.
 use slotmap::{DefaultKey, SlotMap, SparseSecondaryMap};
 
 use crate::geometry::Size;
@@ -17,7 +15,46 @@ use crate::compute::compute_flexbox_layout;
 use crate::compute::compute_grid_layout;
 use crate::compute::{compute_cached_layout, compute_hidden_layout, compute_layout, compute_leaf_layout, round_layout};
 
-use super::{TaffyError, TaffyResult};
+/// The error Taffy generates on invalid operations
+pub type TaffyResult<T> = Result<T, TaffyError>;
+
+/// An error that occurs while trying to access or modify a node's children by index.
+#[derive(Debug)]
+pub enum TaffyError {
+    /// The parent node does not have a child at `child_index`. It only has `child_count` children
+    ChildIndexOutOfBounds {
+        /// The parent node whose child was being looked up
+        parent: NodeId,
+        /// The index that was looked up
+        child_index: usize,
+        /// The total number of children the parent has
+        child_count: usize,
+    },
+    /// The parent node was not found in the [`Taffy`](crate::Taffy) instance.
+    InvalidParentNode(NodeId),
+    /// The child node was not found in the [`Taffy`](crate::Taffy) instance.
+    InvalidChildNode(NodeId),
+    /// The supplied node was not found in the [`Taffy`](crate::Taffy) instance.
+    InvalidInputNode(NodeId),
+}
+
+impl core::fmt::Display for TaffyError {
+    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
+        match self {
+            TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count } => {
+                write!(f, "Index (is {child_index}) should be < child_count ({child_count}) for parent node {parent:?}")
+            }
+            TaffyError::InvalidParentNode(parent) => {
+                write!(f, "Parent Node {parent:?} is not in the Taffy instance")
+            }
+            TaffyError::InvalidChildNode(child) => write!(f, "Child Node {child:?} is not in the Taffy instance"),
+            TaffyError::InvalidInputNode(node) => write!(f, "Supplied Node {node:?} is not in the Taffy instance"),
+        }
+    }
+}
+
+#[cfg(feature = "std")]
+impl std::error::Error for TaffyError {}
 
 /// Global configuration values for a Taffy instance
 pub(crate) struct TaffyConfig {
@@ -158,22 +195,11 @@ where
                     let node_key = node.into();
                     let style = &tree.taffy.nodes[node_key].style;
                     let needs_measure = tree.taffy.nodes[node_key].needs_measure;
-                    if needs_measure {
-                        let node_context = Some(&mut tree.taffy.node_context_data[node_key]);
-                        compute_leaf_layout(
-                            inputs,
-                            style,
-                            Some(|known_dimensions, available_space| {
-                                (tree.measure_function)(known_dimensions, available_space, node, node_context)
-                            }),
-                        )
-                    } else {
-                        /// Type inference gets confused because we're just passing None here. So we give
-                        /// it a concrete type to work with (even though we never construct the inner type)
-                        type DummyMeasureFunction = Option<fn(Size<Option<f32>>, Size<AvailableSpace>) -> Size<f32>>;
-                        let measure_function: DummyMeasureFunction = None;
-                        compute_leaf_layout(inputs, style, measure_function)
-                    }
+                    let measure_function = needs_measure.then_some(|known_dimensions, available_space| {
+                        let node_context = tree.taffy.node_context_data.get_mut(node_key);
+                        (tree.measure_function)(known_dimensions, available_space, node, node_context)
+                    });
+                    compute_leaf_layout(inputs, style, measure_function)
                 }
             }
         })
diff --git a/src/tree/taffy_tree/error.rs b/src/tree/taffy_tree/error.rs
deleted file mode 100644
index ffee3ac94..000000000
--- a/src/tree/taffy_tree/error.rs
+++ /dev/null
@@ -1,47 +0,0 @@
-//! The Error types produced by Taffy.
-#[cfg(feature = "std")]
-use core::fmt::{Display, Formatter, Result};
-
-use crate::tree::NodeId;
-
-/// The error Taffy generates on invalid operations
-pub type TaffyResult<T> = core::result::Result<T, TaffyError>;
-
-/// An error that occurs while trying to access or modify a node's children by index.
-#[derive(Debug)]
-pub enum TaffyError {
-    /// The parent node does not have a child at `child_index`. It only has `child_count` children
-    ChildIndexOutOfBounds {
-        /// The parent node whose child was being looked up
-        parent: NodeId,
-        /// The index that was looked up
-        child_index: usize,
-        /// The total number of children the parent has
-        child_count: usize,
-    },
-    /// The parent node was not found in the [`Taffy`](crate::Taffy) instance.
-    InvalidParentNode(NodeId),
-    /// The child node was not found in the [`Taffy`](crate::Taffy) instance.
-    InvalidChildNode(NodeId),
-    /// The supplied node was not found in the [`Taffy`](crate::Taffy) instance.
-    InvalidInputNode(NodeId),
-}
-
-#[cfg(feature = "std")]
-impl Display for TaffyError {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        match self {
-            TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count } => {
-                write!(f, "Index (is {child_index}) should be < child_count ({child_count}) for parent node {parent:?}")
-            }
-            TaffyError::InvalidParentNode(parent) => {
-                write!(f, "Parent Node {parent:?} is not in the Taffy instance")
-            }
-            TaffyError::InvalidChildNode(child) => write!(f, "Child Node {child:?} is not in the Taffy instance"),
-            TaffyError::InvalidInputNode(node) => write!(f, "Supplied Node {node:?} is not in the Taffy instance"),
-        }
-    }
-}
-
-#[cfg(feature = "std")]
-impl std::error::Error for TaffyError {}
diff --git a/src/tree/taffy_tree/mod.rs b/src/tree/taffy_tree/mod.rs
deleted file mode 100644
index b4afa5f44..000000000
--- a/src/tree/taffy_tree/mod.rs
+++ /dev/null
@@ -1,7 +0,0 @@
-//! Contains the default implementation of [LayoutTree](crate::tree::LayoutTree), [Taffy](crate::tree::Taffy), and the error type for Taffy.
-
-mod error;
-mod tree;
-
-pub use error::{TaffyError, TaffyResult};
-pub use tree::Taffy;