Skip to content

Commit 7ca4609

Browse files
committed
Fix child and child_count methods to use resolved children
1 parent 6657180 commit 7ca4609

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/compute/taffy_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ fn round_layout(tree: &mut Taffy, node_id: NodeId, cumulative_x: f32, cumulative
307307
layout.size.width = round(cumulative_x + unrounded_layout.size.width) - round(cumulative_x);
308308
layout.size.height = round(cumulative_y + unrounded_layout.size.height) - round(cumulative_y);
309309

310-
let child_count = tree.child_count(node_id).unwrap();
310+
let child_count = tree.children[node_id.into()].len();
311311
for index in 0..child_count {
312-
let child = tree.child(node_id, index);
312+
let child = tree.children[node_id.into()][index];
313313
round_layout(tree, child, cumulative_x, cumulative_y);
314314
}
315315
}

src/tree/taffy_tree/tree.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ fn find_children_recursive(tree: &Taffy, node: NodeId, out: &mut Vec<NodeId>) {
108108
}
109109
}
110110

111-
impl LayoutTree for Taffy {
112-
type ChildIter<'a> = RefCellVecIter<'a>;
113-
114-
#[inline(always)]
115-
fn children(&self, node: NodeId) -> Self::ChildIter<'_> {
111+
impl Taffy {
112+
/// Returns the resolved children, taking into account `Display::Contents`
113+
/// Will use cached result if available, else compute and cache.
114+
fn resolve_children(&self, node: NodeId) -> RefMut<'_, Vec<NodeId>> {
116115
let mut cache = self.node_children_cache.borrow_mut();
117116

118117
// If the cache key does not match the requested node_id, then recompute the children for
@@ -124,12 +123,25 @@ impl LayoutTree for Taffy {
124123
}
125124

126125
// In all cases, return a reference into the cache
127-
RefCellVecIter { children: RefMut::map(cache, |c| &mut c.children), index: 0 }
126+
RefMut::map(cache, |c| &mut c.children)
127+
}
128+
}
129+
130+
impl LayoutTree for Taffy {
131+
type ChildIter<'a> = RefCellVecIter<'a>;
132+
133+
#[inline(always)]
134+
fn children(&self, node: NodeId) -> Self::ChildIter<'_> {
135+
RefCellVecIter { children: self.resolve_children(node), index: 0 }
128136
}
129137

130138
#[inline(always)]
131139
fn child_count(&self, node: NodeId) -> usize {
132-
self.children[node.into()].len()
140+
self.resolve_children(node).len()
141+
}
142+
#[inline(always)]
143+
fn child(&self, node: NodeId, id: usize) -> NodeId {
144+
self.resolve_children(node)[id]
133145
}
134146

135147
#[inline(always)]
@@ -155,11 +167,6 @@ impl LayoutTree for Taffy {
155167
}
156168
}
157169

158-
#[inline(always)]
159-
fn child(&self, node: NodeId, id: usize) -> NodeId {
160-
self.children[node.into()][id]
161-
}
162-
163170
#[inline(always)]
164171
fn measure_child_size(
165172
&mut self,

0 commit comments

Comments
 (0)