Skip to content

Commit 2ef49c4

Browse files
authored
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
1 parent 380840a commit 2ef49c4

File tree

5 files changed

+17
-50
lines changed

5 files changed

+17
-50
lines changed

RELEASES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Removed
66

77
- `layout_flexbox()` has been removed from the prelude. Use `FlexboxAlgorithm::perform_layout()` instead.
8+
- 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`.
89

910
### Changes
1011

src/compute/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn compute_node_layout(
139139
println!();
140140

141141
// First we check if we have a cached result for the given input
142-
let cache_run_mode = if tree.is_childless(node) { RunMode::PeformLayout } else { run_mode };
142+
let cache_run_mode = if tree.child_count(node) == 0 { RunMode::PeformLayout } else { run_mode };
143143
if let Some(cached_size_and_baselines) =
144144
compute_from_cache(tree, node, known_dimensions, available_space, cache_run_mode)
145145
{
@@ -182,7 +182,7 @@ fn compute_node_layout(
182182
}
183183

184184
let display_mode = tree.style(node).display;
185-
let has_children = !tree.is_childless(node);
185+
let has_children = tree.child_count(node) != 0;
186186
let computed_size_and_baselines = match (display_mode, has_children) {
187187
(Display::None, _) => perform_computations::<HiddenAlgorithm>(
188188
tree,

src/debug.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ use core::fmt::{Debug, Display, Write};
22
use slotmap::Key;
33
use std::sync::Mutex;
44

5-
use crate::node::Node;
5+
use crate::node::{Node, Taffy};
66
use crate::style;
7-
use crate::tree::LayoutTree;
87

98
/// Prints a debug representation of the computed layout for a tree of nodes, starting with the passed root node.
10-
pub fn print_tree(tree: &impl LayoutTree, root: Node) {
9+
pub fn print_tree(tree: &Taffy, root: Node) {
1110
println!("TREE");
1211
print_node(tree, root, false, String::new());
1312
}
1413

15-
fn print_node(tree: &impl LayoutTree, node: Node, has_sibling: bool, lines_string: String) {
16-
let layout = tree.layout(node);
17-
let style = tree.style(node);
18-
19-
let num_children = tree.child_count(node);
14+
fn print_node(tree: &Taffy, node: Node, has_sibling: bool, lines_string: String) {
15+
let layout = &tree.nodes[node].layout;
16+
let style = &tree.nodes[node].style;
17+
let num_children = tree.children[node].len();
2018

2119
let display = match (num_children, style.display) {
2220
(_, style::Display::None) => "NONE",
@@ -43,7 +41,7 @@ fn print_node(tree: &impl LayoutTree, node: Node, has_sibling: bool, lines_strin
4341
let new_string = lines_string + bar;
4442

4543
// Recurse into children
46-
for (index, child) in tree.children(node).enumerate() {
44+
for (index, child) in tree.children[node].iter().enumerate() {
4745
let has_sibling = index < num_children - 1;
4846
print_node(tree, *child, has_sibling, new_string.clone());
4947
}

src/node.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,14 @@ impl LayoutTree for Taffy {
8383
self.children[node].len()
8484
}
8585

86-
fn is_childless(&self, node: Node) -> bool {
87-
self.children[node].is_empty()
88-
}
89-
90-
fn parent(&self, node: Node) -> Option<Node> {
91-
self.parents.get(node).copied().flatten()
92-
}
93-
9486
fn style(&self, node: Node) -> &Style {
9587
&self.nodes[node].style
9688
}
9789

98-
fn layout(&self, node: Node) -> &Layout {
99-
&self.nodes[node].layout
100-
}
101-
10290
fn layout_mut(&mut self, node: Node) -> &mut Layout {
10391
&mut self.nodes[node].layout
10492
}
10593

106-
#[inline(always)]
107-
fn mark_dirty(&mut self, node: Node) -> TaffyResult<()> {
108-
self.mark_dirty_internal(node)
109-
}
110-
11194
fn measure_node(
11295
&self,
11396
node: Node,
@@ -242,7 +225,7 @@ impl Taffy {
242225
self.measure_funcs.remove(node);
243226
}
244227

245-
self.mark_dirty_internal(node)?;
228+
self.mark_dirty(node)?;
246229

247230
Ok(())
248231
}
@@ -251,7 +234,7 @@ impl Taffy {
251234
pub fn add_child(&mut self, parent: Node, child: Node) -> TaffyResult<()> {
252235
self.parents[child] = Some(parent);
253236
self.children[parent].push(child);
254-
self.mark_dirty_internal(parent)?;
237+
self.mark_dirty(parent)?;
255238

256239
Ok(())
257240
}
@@ -270,7 +253,7 @@ impl Taffy {
270253

271254
self.children[parent] = children.iter().copied().collect::<_>();
272255

273-
self.mark_dirty_internal(parent)?;
256+
self.mark_dirty(parent)?;
274257

275258
Ok(())
276259
}
@@ -295,7 +278,7 @@ impl Taffy {
295278
let child = self.children[parent].remove(child_index);
296279
self.parents[child] = None;
297280

298-
self.mark_dirty_internal(parent)?;
281+
self.mark_dirty(parent)?;
299282

300283
Ok(child)
301284
}
@@ -313,7 +296,7 @@ impl Taffy {
313296
let old_child = core::mem::replace(&mut self.children[parent][child_index], new_child);
314297
self.parents[old_child] = None;
315298

316-
self.mark_dirty_internal(parent)?;
299+
self.mark_dirty(parent)?;
317300

318301
Ok(old_child)
319302
}
@@ -341,7 +324,7 @@ impl Taffy {
341324
/// Sets the [`Style`] of the provided `node`
342325
pub fn set_style(&mut self, node: Node, style: Style) -> TaffyResult<()> {
343326
self.nodes[node].style = style;
344-
self.mark_dirty_internal(node)?;
327+
self.mark_dirty(node)?;
345328
Ok(())
346329
}
347330

@@ -360,7 +343,7 @@ impl Taffy {
360343
/// Performs a recursive depth-first search up the tree until the root node is reached
361344
///
362345
/// WARNING: this will stack-overflow if the tree contains a cycle
363-
fn mark_dirty_internal(&mut self, node: Node) -> TaffyResult<()> {
346+
pub fn mark_dirty(&mut self, node: Node) -> TaffyResult<()> {
364347
/// WARNING: this will stack-overflow if the tree contains a cycle
365348
fn mark_dirty_recursive(
366349
nodes: &mut SlotMap<Node, NodeData>,

src/tree.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use slotmap::DefaultKey;
44

55
use crate::{
6-
error::TaffyResult,
76
layout::{Cache, Layout},
87
prelude::*,
98
};
@@ -24,32 +23,18 @@ pub trait LayoutTree {
2423
/// Get the number of children for the given node
2524
fn child_count(&self, node: Node) -> usize;
2625

27-
/// Returns true if the node has no children
28-
fn is_childless(&self, node: Node) -> bool;
29-
3026
/// Get a specific child of a node, where the index represents the nth child
3127
fn child(&self, node: Node, index: usize) -> Node;
3228

33-
/// Get any available parent for this node
34-
fn parent(&self, node: Node) -> Option<Node>;
35-
3629
// todo: allow abstractions over this so we don't prescribe how layout works
3730
// for reference, CSS cascades require context, and storing a full flexbox layout for each node could be inefficient
3831
//
3932
/// Get the [`Style`] for this Node.
4033
fn style(&self, node: Node) -> &Style;
4134

42-
/// Get the node's output "Final Layout"
43-
fn layout(&self, node: Node) -> &Layout;
44-
4535
/// Modify the node's output layout
4636
fn layout_mut(&mut self, node: Node) -> &mut Layout;
4737

48-
/// Mark a node as dirty to tell Taffy that something has changed and it needs to be recomputed.
49-
///
50-
/// Commonly done if the style of the node has changed.
51-
fn mark_dirty(&mut self, node: Node) -> TaffyResult<()>;
52-
5338
/// Measure a node. Taffy uses this to force reflows of things like text and overflowing content.
5439
fn measure_node(
5540
&self,

0 commit comments

Comments
 (0)