Remove unused methods from the LayoutTree trait (#425)
* Remove unused methods from the LayoutTree trait
Removes:
- parent (unused)
- mark_dirty (make method on Taffy struct public)
- is_childless (use child_count instead)
- layout (use &Taffy instead of &LayoutTree for debug printing)
* cargo fmt
* Add to RElEASES.md
2ef49c4ae8c1a563ae73e67111358a6a8d8e03e4 · by Nico Burns · (3 years ago) · parent 380840a8f · browse the tree at this commit
diff --git a/RELEASES.md b/RELEASES.md
index ce4556c3e..57714de46 100644
--- a/RELEASES.md
+++ b/RELEASES.md
@@ -5,6 +5,7 @@
### Removed
- `layout_flexbox()` has been removed from the prelude. Use `FlexboxAlgorithm::perform_layout()` instead.
+- The following methods have been removed from the `LayoutTree` trait: `parent`, `is_childless`, `layout`, and `mark_dirty`. These no longer need to be implemented in custom implementation of `LayoutTree`.
### Changes
diff --git a/src/compute/mod.rs b/src/compute/mod.rs
index 0fa23fae8..fa7e71e54 100644
--- a/src/compute/mod.rs
+++ b/src/compute/mod.rs
@@ -139,7 +139,7 @@ fn compute_node_layout(
println!();
// First we check if we have a cached result for the given input
- let cache_run_mode = if tree.is_childless(node) { RunMode::PeformLayout } else { run_mode };
+ let cache_run_mode = if tree.child_count(node) == 0 { RunMode::PeformLayout } else { run_mode };
if let Some(cached_size_and_baselines) =
compute_from_cache(tree, node, known_dimensions, available_space, cache_run_mode)
{
@@ -182,7 +182,7 @@ fn compute_node_layout(
}
let display_mode = tree.style(node).display;
- let has_children = !tree.is_childless(node);
+ let has_children = tree.child_count(node) != 0;
let computed_size_and_baselines = match (display_mode, has_children) {
(Display::None, _) => perform_computations::<HiddenAlgorithm>(
tree,
diff --git a/src/debug.rs b/src/debug.rs
index b95fa4703..72592e5e4 100644
--- a/src/debug.rs
+++ b/src/debug.rs
@@ -2,21 +2,19 @@ use core::fmt::{Debug, Display, Write};
use slotmap::Key;
use std::sync::Mutex;
-use crate::node::Node;
+use crate::node::{Node, Taffy};
use crate::style;
-use crate::tree::LayoutTree;
/// Prints a debug representation of the computed layout for a tree of nodes, starting with the passed root node.
-pub fn print_tree(tree: &impl LayoutTree, root: Node) {
+pub fn print_tree(tree: &Taffy, root: Node) {
println!("TREE");
print_node(tree, root, false, String::new());
}
-fn print_node(tree: &impl LayoutTree, node: Node, has_sibling: bool, lines_string: String) {
- let layout = tree.layout(node);
- let style = tree.style(node);
-
- let num_children = tree.child_count(node);
+fn print_node(tree: &Taffy, node: Node, has_sibling: bool, lines_string: String) {
+ let layout = &tree.nodes[node].layout;
+ let style = &tree.nodes[node].style;
+ let num_children = tree.children[node].len();
let display = match (num_children, style.display) {
(_, style::Display::None) => "NONE",
@@ -43,7 +41,7 @@ fn print_node(tree: &impl LayoutTree, node: Node, has_sibling: bool, lines_strin
let new_string = lines_string + bar;
// Recurse into children
- for (index, child) in tree.children(node).enumerate() {
+ for (index, child) in tree.children[node].iter().enumerate() {
let has_sibling = index < num_children - 1;
print_node(tree, *child, has_sibling, new_string.clone());
}
diff --git a/src/node.rs b/src/node.rs
index 500f9d9eb..854c76e41 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -83,31 +83,14 @@ impl LayoutTree for Taffy {
self.children[node].len()
}
- fn is_childless(&self, node: Node) -> bool {
- self.children[node].is_empty()
- }
-
- fn parent(&self, node: Node) -> Option<Node> {
- self.parents.get(node).copied().flatten()
- }
-
fn style(&self, node: Node) -> &Style {
&self.nodes[node].style
}
- fn layout(&self, node: Node) -> &Layout {
- &self.nodes[node].layout
- }
-
fn layout_mut(&mut self, node: Node) -> &mut Layout {
&mut self.nodes[node].layout
}
- #[inline(always)]
- fn mark_dirty(&mut self, node: Node) -> TaffyResult<()> {
- self.mark_dirty_internal(node)
- }
-
fn measure_node(
&self,
node: Node,
@@ -242,7 +225,7 @@ impl Taffy {
self.measure_funcs.remove(node);
}
- self.mark_dirty_internal(node)?;
+ self.mark_dirty(node)?;
Ok(())
}
@@ -251,7 +234,7 @@ impl Taffy {
pub fn add_child(&mut self, parent: Node, child: Node) -> TaffyResult<()> {
self.parents[child] = Some(parent);
self.children[parent].push(child);
- self.mark_dirty_internal(parent)?;
+ self.mark_dirty(parent)?;
Ok(())
}
@@ -270,7 +253,7 @@ impl Taffy {
self.children[parent] = children.iter().copied().collect::<_>();
- self.mark_dirty_internal(parent)?;
+ self.mark_dirty(parent)?;
Ok(())
}
@@ -295,7 +278,7 @@ impl Taffy {
let child = self.children[parent].remove(child_index);
self.parents[child] = None;
- self.mark_dirty_internal(parent)?;
+ self.mark_dirty(parent)?;
Ok(child)
}
@@ -313,7 +296,7 @@ impl Taffy {
let old_child = core::mem::replace(&mut self.children[parent][child_index], new_child);
self.parents[old_child] = None;
- self.mark_dirty_internal(parent)?;
+ self.mark_dirty(parent)?;
Ok(old_child)
}
@@ -341,7 +324,7 @@ impl Taffy {
/// Sets the [`Style`] of the provided `node`
pub fn set_style(&mut self, node: Node, style: Style) -> TaffyResult<()> {
self.nodes[node].style = style;
- self.mark_dirty_internal(node)?;
+ self.mark_dirty(node)?;
Ok(())
}
@@ -360,7 +343,7 @@ impl Taffy {
/// Performs a recursive depth-first search up the tree until the root node is reached
///
/// WARNING: this will stack-overflow if the tree contains a cycle
- fn mark_dirty_internal(&mut self, node: Node) -> TaffyResult<()> {
+ pub fn mark_dirty(&mut self, node: Node) -> TaffyResult<()> {
/// WARNING: this will stack-overflow if the tree contains a cycle
fn mark_dirty_recursive(
nodes: &mut SlotMap<Node, NodeData>,
diff --git a/src/tree.rs b/src/tree.rs
index 0a6448a47..47bb7fbd5 100644
--- a/src/tree.rs
+++ b/src/tree.rs
@@ -3,7 +3,6 @@
use slotmap::DefaultKey;
use crate::{
- error::TaffyResult,
layout::{Cache, Layout},
prelude::*,
};
@@ -24,32 +23,18 @@ pub trait LayoutTree {
/// Get the number of children for the given node
fn child_count(&self, node: Node) -> usize;
- /// Returns true if the node has no children
- fn is_childless(&self, node: Node) -> bool;
-
/// Get a specific child of a node, where the index represents the nth child
fn child(&self, node: Node, index: usize) -> Node;
- /// Get any available parent for this node
- fn parent(&self, node: Node) -> Option<Node>;
-
// todo: allow abstractions over this so we don't prescribe how layout works
// for reference, CSS cascades require context, and storing a full flexbox layout for each node could be inefficient
//
/// Get the [`Style`] for this Node.
fn style(&self, node: Node) -> &Style;
- /// Get the node's output "Final Layout"
- fn layout(&self, node: Node) -> &Layout;
-
/// Modify the node's output layout
fn layout_mut(&mut self, node: Node) -> &mut Layout;
- /// Mark a node as dirty to tell Taffy that something has changed and it needs to be recomputed.
- ///
- /// Commonly done if the style of the node has changed.
- fn mark_dirty(&mut self, node: Node) -> TaffyResult<()>;
-
/// Measure a node. Taffy uses this to force reflows of things like text and overflowing content.
fn measure_node(
&self,