Skip to content

#279 WP3: one LayoutTree wrapper for every taffy access - #309

Merged
samitbasu merged 1 commit into
mainfrom
wp3-tree
Nov 17, 2026
Merged

samitbasu merged 1 commit into
mainfrom
wp3-tree

Conversation

@samitbasu

Copy link
Copy Markdown
Owner

Part of #279
Fixes #284

What

src/layout_tree.rs: a crate-private LayoutTree around TaffyTree<Node>, the one place taffy's TaffyResult is mapped. taffy 0.15 panics on a missing node in its own slotmap indexing rather than returning Err, so the wrapper checks existence first. Every node is made by new_node with its context, so "has a context" is the existence check.

API (all #[track_caller], so the report names the calling site):

  • new(), scratch() (an empty tree that reports to the same context), reset(ctx) (clear, then report to ctx)
  • new_node(style, kids, node) -> NodeId: a missing kid is left out and reported. If taffy refuses, the result is a dangling id that every accessor treats as missing.
  • node(id) -> Option<&Node>, node_mut(id) -> Option<&mut Node>
  • layout(id) -> Layout (default when missing), style(id) -> &Style (Style::DEFAULT), children(id) -> Vec<NodeId> (empty)
  • set_style(id, style), compute(root, available, measure): no-ops when the node is missing

A miss reports Problem::Internal("taffy: missing node"); a taffy Err reports Problem::Internal("taffy: refused").

leaf_node and container return their NodeId. handle(node) takes it, so the kids.last().expect sites are gone.

Sites converted

  • builder.rs: 19 expects (4 of them kids.last) and 5 Result-handling sites
  • pipeline.rs: 18 expects and 2 Result-handling sites
  • collapse.rs: 5 expects, plus 3 in a test
  • region.rs: 4 expects
  • vlist.rs: 3 expects and 1 Result-handling site
  • scroll.rs: 2 Result-handling sites

That is 49 library expects. measure, roots, stack, draw and debug make no taffy-tree accesses.

Reporting

Every accessor reports, to the context of the build that last reset the tree. A scratch tree inherits that context. The only silent case is a tree that has never been reset: before the first build, and in unit tests that never call reset.

Behaviour

For a well-formed tree, nothing changes. CI is green (cargo xtask ci exit 0) and no golden changed. The max_content_width candidate root now carries a bare Root node like every other node; its layout is the same.

When a child is missing, collect returns early, and the parent's reach widening (widen_reach) skips the missing child instead of indexing it.

Left for WP4 (structural expects kept as-is)

the node is identified (builder NodeHandle::id), a context menu needs an identified node, a scroll leaf has a scroll node (pipeline solve_content), a list's container is a scroll container (vlist view), a root's body was built, a scroll container is identified (pipeline collect), the node is inside the root (pipeline reachable).

Likely conflicts with #300

In builder.rs: attach_draw (both rewrite it), region() (the kids.last/leaf_text lines sit next to #300's add_region), the collapsible match step (#300 edits exited.push inside it), and the window root. In region.rs: the use taffy import line. Each is small and mechanical.

🤖 Generated with Claude Code

taffy 0.15 returns TaffyResult everywhere but panics on a missing node
through its own slotmap indexing, so the 49 `.expect("node exists" …)`
sites could never have been handled at the call. `LayoutTree` wraps the
tree and is the one place that maps taffy's results. Every node is made
by `new_node(style, kids, node)` with its context, so "has a context" is
the existence check made before each taffy call.

A miss reports Problem::Internal("taffy: missing node") (a refusal,
"taffy: refused") at the caller, to the context of the last build
(reset/scratch), and falls back: a default Layout and Style, no
children, no node, and a no-op set_style/compute. A missing child is
left out of new_node.

leaf_node and container return their NodeId, removing the
`kids.last().expect` sites.

No behaviour change for a well-formed tree; no golden changed. The
max_content_width candidate root now carries a bare Root node so it
passes the existence check, like every other node.

Part of #279. Fixes #284.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKe8zfqGfVMCSC5L3up
@samitbasu

Copy link
Copy Markdown
Owner Author

Gate (my run on de8e7f6 with current main): cargo xtask ci (with fuzz) exits 0. expect/unwrap sites go from 67 to 15 across builder, pipeline, collapse, region, vlist and scroll. The remainder are non-taffy sites left for WP4. layout_tree.rs has no panic sites outside tests. No API change; no golden changed. Merging (the user authorised merges).

@samitbasu
samitbasu merged commit ccaab8d into main Nov 17, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#279 WP3: a LayoutTree wrapper whose accessors can't fail (taffy expects)

1 participant