dioxus-core 0.8.0-alpha.3

Build fullstack web, desktop, and mobile apps with a single codebase.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//! Mount-table bookkeeping for committed vnodes.
//!
//! Invariants maintained here:
//! - A live `MountId` owns one committed `VNode`, one render parent, one logical parent, and one
//!   render target.
//! - Anchor slots and dynamic slots are sized from the committed vnode template and remain stable
//!   until `commit_mount`.
//! - Non-empty fragment dynamic slots point at an exact contiguous range in
//!   `fragment_child_mounts`; empty fragments store an empty slot.
//! - Diff internals must use `mounted_fragment_children_exact` when vnode shape says a fragment has
//!   children. The permissive `mounted_fragment_children` accessor is for public inspection paths
//!   where "not a fragment" should produce an empty list.

use crate::{
    DynamicNode, DynamicNodeSlot, RenderTargetId, ScopeId, VNode,
    arena::{MountId, MountedDynamicNodeSlot, MountedElementId},
    virtual_dom::VirtualDom,
};

/// Whether a mount is allowed to write renderer mutations.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum RenderMode {
    Foreground,
    Background,
}

/// This is a union that stores either:
/// - A scope id
/// - A mounted element id
/// - A fragment range
///
/// The discriminate is determined by the dynamic node variant.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PackedMountedSlot {
    value: usize,
}

const UNWRITTEN_FRAGMENT_CHILD_MOUNT: MountId = MountId(usize::MAX);

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct FragmentMountWriter {
    mount: MountId,
    anchor_index: usize,
    dyn_node_idx: usize,
    start: usize,
    len: usize,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct MountedParent {
    parent: MountId,
    anchor_index: usize,
    slot_index: usize,
    kind: MountedParentKind,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum MountedParentKind {
    ComponentRoot,
    /// `child_index` is this child's slot in its parent fragment's committed child list. Placement
    /// indexes the committed (old) child list with it, so it must track the *committed* position:
    /// it is advanced to a child's new index only when the fragment diff commits, never mid-diff.
    FragmentChild {
        child_index: usize,
    },
}

impl MountedParent {
    pub(crate) fn component_root(parent: MountId, anchor_index: usize, slot_index: usize) -> Self {
        Self {
            parent,
            anchor_index,
            slot_index,
            kind: MountedParentKind::ComponentRoot,
        }
    }

    pub(crate) fn fragment_child(
        parent: MountId,
        anchor_index: usize,
        slot_index: usize,
        child_index: usize,
    ) -> Self {
        Self {
            parent,
            anchor_index,
            slot_index,
            kind: MountedParentKind::FragmentChild { child_index },
        }
    }

    pub(crate) fn parent(self) -> MountId {
        self.parent
    }

    pub(crate) fn anchor_index(self) -> usize {
        self.anchor_index
    }

    pub(crate) fn slot<'a>(self, vnode: &'a VNode) -> Option<DynamicNodeSlot<'a>> {
        vnode.dynamic_node_slot(self.anchor_index, self.slot_index)
    }

    pub(crate) fn kind(self) -> MountedParentKind {
        self.kind
    }
}

#[cfg(debug_assertions)]
impl FragmentMountWriter {
    pub(crate) fn len(self) -> usize {
        self.len
    }
}

impl PackedMountedSlot {
    const fn empty() -> Self {
        Self { value: 0 }
    }

    fn from_mounted_element(id: MountedElementId) -> Self {
        Self::from_index(id.index())
    }

    fn from_slot(slot: MountedDynamicNodeSlot) -> Self {
        match slot {
            MountedDynamicNodeSlot::Empty => Self::empty(),
            MountedDynamicNodeSlot::Text(id) => Self::from_index(id.index()),
            MountedDynamicNodeSlot::Component(scope) => Self::from_index(scope.index()),
            MountedDynamicNodeSlot::Fragment(start) => Self::from_index(start),
        }
    }

    fn from_index(index: usize) -> Self {
        Self { value: index + 1 }
    }

    fn mounted_element(self) -> Option<MountedElementId> {
        self.value.checked_sub(1).map(MountedElementId::from_index)
    }

    fn text(self) -> Option<MountedElementId> {
        self.mounted_element()
    }

    fn component_scope(self) -> Option<ScopeId> {
        self.value.checked_sub(1).map(ScopeId::new)
    }

    fn fragment_start(self) -> Option<usize> {
        self.value.checked_sub(1)
    }

    fn set_component_scope(&mut self, scope: ScopeId) {
        *self = Self::from_slot(MountedDynamicNodeSlot::Component(scope));
    }
}

pub(crate) struct Mount {
    render_parent: Option<MountedParent>,

    logical_parent: Option<MountId>,

    target_id: RenderTargetId,

    node: VNode,

    mode: RenderMode,

    /// Mounted slots for this node in one allocation, laid out as two regions:
    /// `[ anchor slots (anchor_count) | dynamic slots (rest) ]`.
    /// Previously these were three separate `Box<[_]>` - one heap allocation per region, so three
    /// allocs/frees per mounted node. A list of N rows is N mounts, so that tripled the per-node
    /// allocator traffic on both create and removal. One backing slice means one alloc/free.
    slots: Box<[PackedMountedSlot]>,
    anchor_count: u32,

    fragment_child_mounts: Vec<MountId>,
}

impl Mount {
    pub(crate) fn new(
        node: VNode,
        logical_parent: Option<MountId>,
        target_id: RenderTargetId,
    ) -> Self {
        // Anchor and dynamic-node slot counts are structural: the anchor array length is the
        // number of template anchors, and the dynamic array length is the number of runtime
        // dynamic nodes. Static roots are stored in their structural root anchors.
        let anchor_count = node.template().anchors().len();
        let dynamic_count = node.dynamic_node_values().len();
        Self {
            render_parent: None,
            logical_parent,
            target_id,
            node,
            mode: RenderMode::Foreground,
            slots: vec![PackedMountedSlot::empty(); anchor_count + dynamic_count].into(),
            anchor_count: anchor_count as u32,
            fragment_child_mounts: Vec::new(),
        }
    }

    pub(crate) fn mounted_anchor_node(&self, idx: usize) -> Option<MountedElementId> {
        self.anchor_slot(idx).mounted_element()
    }

    pub(crate) fn logical_parent(&self) -> Option<MountId> {
        self.logical_parent
    }

    pub(crate) fn render_parent(&self) -> Option<MountedParent> {
        self.render_parent
    }

    pub(crate) fn node(&self) -> &VNode {
        &self.node
    }

    fn dynamic_node_anchor_index(&self, dyn_node_idx: usize) -> usize {
        self.node
            .dynamic_anchors()
            .find(|anchor| anchor.nodes().any(|slot| slot.index() == dyn_node_idx))
            .map(|anchor| anchor.anchor_index())
            .expect("dynamic node should belong to an anchor")
    }

    fn dynamic_offset(&self) -> usize {
        self.anchor_count as usize
    }

    fn dynamic_slot(&self, idx: usize) -> PackedMountedSlot {
        self.slots[self.dynamic_offset() + idx]
    }

    fn dynamic_slot_mut(&mut self, idx: usize) -> &mut PackedMountedSlot {
        let offset = self.dynamic_offset();
        &mut self.slots[offset + idx]
    }

    fn anchor_slot(&self, idx: usize) -> PackedMountedSlot {
        self.slots[idx]
    }

    fn anchor_slot_mut(&mut self, idx: usize) -> &mut PackedMountedSlot {
        &mut self.slots[idx]
    }
}

impl VirtualDom {
    pub(crate) fn create_mount(
        &mut self,
        node: &VNode,
        logical_parent: Option<MountId>,
        target_id: RenderTargetId,
    ) -> MountId {
        let mut mounts = self.runtime.mounts.borrow_mut();
        let entry = mounts.vacant_entry();
        let mount = MountId(entry.key());
        entry.insert(Mount::new(node.clone(), logical_parent, target_id));

        mount
    }

    /// Reuse an existing mount's allocation (keeping its element ids and child
    /// component/scope slots) by re-parenting it. Paired with `finish_create` to
    /// re-emit a background-created subtree to the foreground writer without
    /// allocating fresh scopes.
    pub(crate) fn reuse_mount(
        &mut self,
        mount: MountId,
        render_parent: Option<MountId>,
        logical_parent: Option<MountId>,
        target_id: RenderTargetId,
    ) {
        self.with_mount_mut(mount, |mount| {
            mount.render_parent = match (mount.render_parent, render_parent) {
                (Some(current), Some(parent)) if current.parent() == parent => Some(current),
                _ => None,
            };
            mount.logical_parent = logical_parent;
            mount.target_id = target_id;
        });
    }

    pub(crate) fn remove_mount(&mut self, mount: MountId) {
        self.runtime.mounts.borrow_mut().remove(mount.0);
    }

    pub(crate) fn mount_target_id(&self, mount: MountId) -> RenderTargetId {
        self.with_mount(mount, |mount| mount.target_id)
    }

    pub(crate) fn mounted_render_parent(&self, mount: MountId) -> Option<MountedParent> {
        self.with_mount(mount, |mount| mount.render_parent())
    }

    pub(crate) fn set_mounted_render_parent(&self, mount: MountId, render_parent: MountedParent) {
        self.with_mount_mut(mount, |mount| {
            mount.render_parent = Some(render_parent);
        });
    }

    pub(crate) fn mounted_logical_parent(&self, mount: MountId) -> Option<MountId> {
        self.with_mount(mount, |mount| mount.logical_parent)
    }

    pub(crate) fn copy_render_parent_slot(&self, from: MountId, to: MountId) {
        let mut mounts = self.runtime.mounts.borrow_mut();
        let render_parent = mounts[from.0].render_parent();
        mounts[to.0].render_parent = render_parent;
    }

    pub(crate) fn mounted_anchor_node(
        &self,
        mount: MountId,
        anchor_idx: usize,
    ) -> Option<MountedElementId> {
        self.with_mount(mount, |mount| {
            mount.anchor_slot(anchor_idx).mounted_element()
        })
    }

    #[track_caller]
    pub(crate) fn unchecked_mounted_anchor_node(
        &self,
        mount: MountId,
        anchor_idx: usize,
    ) -> MountedElementId {
        self.mounted_anchor_node(mount, anchor_idx)
            .expect("anchor slot")
    }

    pub(crate) fn set_mounted_anchor_node(
        &self,
        mount: MountId,
        anchor_idx: usize,
        value: MountedElementId,
    ) {
        self.with_mount_mut(mount, |mount| {
            *mount.anchor_slot_mut(anchor_idx) = PackedMountedSlot::from_mounted_element(value);
        });
    }

    pub(crate) fn clear_mounted_anchor_node(&self, mount: MountId, anchor_idx: usize) {
        self.with_mount_mut(mount, |mount| {
            *mount.anchor_slot_mut(anchor_idx) = PackedMountedSlot::empty();
        });
    }

    pub(crate) fn mounted_dynamic_node_slot_snapshot(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
    ) -> PackedMountedSlot {
        self.with_mount(mount, |mount| mount.dynamic_slot(dyn_node_idx))
    }

    pub(crate) fn restore_mounted_dynamic_node_slot(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        value: PackedMountedSlot,
    ) {
        self.set_packed_mounted_dynamic_node_slot(mount, dyn_node_idx, value);
    }

    pub(crate) fn set_mounted_dynamic_node_slot(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        value: MountedDynamicNodeSlot,
    ) {
        self.set_packed_mounted_dynamic_node_slot(
            mount,
            dyn_node_idx,
            PackedMountedSlot::from_slot(value),
        );
    }

    fn set_packed_mounted_dynamic_node_slot(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        value: PackedMountedSlot,
    ) {
        self.with_mount_mut(mount, |mount| {
            *mount.dynamic_slot_mut(dyn_node_idx) = value;
        });
    }

    pub(crate) fn clear_mounted_dynamic_node_slot(&self, mount: MountId, dyn_node_idx: usize) {
        self.set_mounted_dynamic_node_slot(mount, dyn_node_idx, MountedDynamicNodeSlot::Empty);
    }

    pub(crate) fn mounted_dynamic_text_node(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
    ) -> Option<MountedElementId> {
        self.with_mount(mount, |mount| mount.dynamic_slot(dyn_node_idx).text())
    }

    pub(crate) fn unchecked_mounted_dynamic_text_node(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
    ) -> MountedElementId {
        self.mounted_dynamic_text_node(mount, dyn_node_idx)
            .expect("text slot")
    }

    pub(crate) fn set_mounted_dynamic_text_node(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        value: MountedElementId,
    ) {
        self.set_mounted_dynamic_node_slot(
            mount,
            dyn_node_idx,
            MountedDynamicNodeSlot::Text(value),
        );
    }

    pub(crate) fn clear_mounted_fragment_children(&self, mount: MountId, dyn_node_idx: usize) {
        self.with_mount_mut(mount, |mount| {
            *mount.dynamic_slot_mut(dyn_node_idx) = PackedMountedSlot::empty();
        });
    }

    pub(crate) fn begin_mounted_fragment_children(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        len: usize,
    ) -> FragmentMountWriter {
        self.with_mount_mut(mount, |mount_state| {
            let start = mount_state.fragment_child_mounts.len();
            let anchor_index = mount_state.dynamic_node_anchor_index(dyn_node_idx);
            mount_state
                .fragment_child_mounts
                .resize(start + len, UNWRITTEN_FRAGMENT_CHILD_MOUNT);
            FragmentMountWriter {
                mount,
                anchor_index,
                dyn_node_idx,
                start,
                len,
            }
        })
    }

    /// Write `child` into fragment slot `idx` and home its render parent at that slot.
    ///
    /// `idx` becomes the child's committed `child_index`, so this is only correct once the slot is
    /// the child's final position (the create path and the post-diff commit). During a diff, use
    /// [`Self::stage_mounted_fragment_child`] so placement keeps reading the old committed index.
    pub(crate) fn set_mounted_fragment_child(
        &self,
        writer: FragmentMountWriter,
        idx: usize,
        child: MountId,
    ) {
        let render_parent = MountedParent::fragment_child(
            writer.mount,
            writer.anchor_index,
            writer.dyn_node_idx,
            idx,
        );
        let mut mounts = self.stage_mounted_fragment_child_inner(writer, idx, child);
        mounts[child.0].render_parent = Some(render_parent);
    }

    /// Write `child` into the pending fragment slot without re-homing its render parent.
    ///
    /// Used while a fragment diff is in flight: the child's render parent must keep pointing at its
    /// old committed index (which placement reads against the old child list) until the whole diff
    /// commits and [`Self::set_mounted_fragment_child`] advances it to the new index.
    pub(crate) fn stage_mounted_fragment_child(
        &self,
        writer: FragmentMountWriter,
        idx: usize,
        child: MountId,
    ) {
        let _ = self.stage_mounted_fragment_child_inner(writer, idx, child);
    }

    fn stage_mounted_fragment_child_inner(
        &self,
        writer: FragmentMountWriter,
        idx: usize,
        child: MountId,
    ) -> std::cell::RefMut<'_, slab::Slab<Mount>> {
        debug_assert_ne!(
            child, UNWRITTEN_FRAGMENT_CHILD_MOUNT,
            "unwritten fragment child sentinel cannot be a live child mount"
        );
        debug_assert!(
            idx < writer.len,
            "fragment child write index must fit the pending range"
        );
        let mut mounts = self.runtime.mounts.borrow_mut();
        mounts[writer.mount.0].fragment_child_mounts[writer.start + idx] = child;
        mounts
    }

    pub(crate) fn commit_mounted_fragment_children(&self, writer: FragmentMountWriter) {
        self.with_mount_mut(writer.mount, |mount| {
            if writer.len == 0 {
                *mount.dynamic_slot_mut(writer.dyn_node_idx) = PackedMountedSlot::empty();
                return;
            }

            let range = writer.start..writer.start + writer.len;
            debug_assert!(
                range.end <= mount.fragment_child_mounts.len(),
                "fragment child writer range must fit fragment storage"
            );
            debug_assert!(
                mount.fragment_child_mounts[range]
                    .iter()
                    .all(|mount| { *mount != UNWRITTEN_FRAGMENT_CHILD_MOUNT }),
                "fragment child writer range must be fully written before commit"
            );
            *mount.dynamic_slot_mut(writer.dyn_node_idx) =
                PackedMountedSlot::from_slot(MountedDynamicNodeSlot::Fragment(writer.start));
        });
    }

    pub(crate) fn mounted_fragment_children(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        len: usize,
    ) -> Vec<MountId> {
        self.try_with_mounted_fragment_children(mount, dyn_node_idx, len, |children| {
            children.to_vec()
        })
        .expect("non-empty mounted fragment should have a child range")
    }

    pub(crate) fn try_with_mounted_fragment_children<R>(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        len: usize,
        with_children: impl FnOnce(&[MountId]) -> R,
    ) -> Option<R> {
        self.with_mount(mount, |mount| {
            if len == 0 {
                return Some(with_children(&[]));
            }
            let start = mount.dynamic_slot(dyn_node_idx).fragment_start()?;
            mount
                .fragment_child_mounts
                .get(start..start + len)
                .map(with_children)
        })
    }

    /// Return the fragment child mounts for a vnode fragment whose shape is known.
    ///
    /// Invariant: `mount` is live, `dyn_node_idx` is a fragment slot, and `len` is the current
    /// fragment child count from the vnode that owns the slot.
    pub(crate) fn mounted_fragment_children_exact(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        len: usize,
    ) -> Vec<MountId> {
        let children = self.mounted_fragment_children(mount, dyn_node_idx, len);
        assert!(children.len() == len, "fragment slots");
        children
    }

    pub(crate) fn mounted_dynamic_component_scope(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
    ) -> Option<ScopeId> {
        self.with_mount(mount, |mount| {
            mount.dynamic_slot(dyn_node_idx).component_scope()
        })
    }

    pub(crate) fn mounted_dynamic_component_root_mount(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
    ) -> Option<MountId> {
        let scope = self.mounted_dynamic_component_scope(mount, dyn_node_idx)?;
        self.runtime.try_get_state(scope)?.root_mount()
    }

    pub(crate) fn unchecked_mounted_dynamic_component_scope(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
    ) -> ScopeId {
        self.mounted_dynamic_component_scope(mount, dyn_node_idx)
            .expect("component slot")
    }

    pub(crate) fn set_mounted_dynamic_component_scope(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        value: ScopeId,
    ) {
        self.with_mount_mut(mount, |mount| {
            mount
                .dynamic_slot_mut(dyn_node_idx)
                .set_component_scope(value);
        });
    }

    pub(crate) fn set_component_root_render_parent(
        &self,
        mount: MountId,
        dyn_node_idx: usize,
        root_mount: MountId,
    ) {
        let anchor_index =
            self.with_mount(mount, |mount| mount.dynamic_node_anchor_index(dyn_node_idx));
        let render_parent = MountedParent::component_root(mount, anchor_index, dyn_node_idx);
        self.runtime.mounts.borrow_mut()[root_mount.0].render_parent = Some(render_parent);
    }

    pub(crate) fn current_mounted_view(&self, mount: MountId) -> Option<VNode> {
        self.runtime
            .mounts
            .borrow()
            .get(mount.0)
            .map(|mount| mount.node.clone())
    }

    pub(crate) fn set_mount_mode(&self, mount: MountId, mode: RenderMode) {
        self.with_mount_mut(mount, |mount| mount.mode = mode);
    }

    pub(crate) fn mount_should_render(&self, mount: MountId) -> bool {
        self.with_mount(mount, |mount| mount.mode == RenderMode::Foreground)
    }

    /// Commit the new vnode for a mount after its roots/dynamic slots have been updated.
    ///
    /// Invariant: slot-writing paths and component root replacement paths update render parents
    /// before commit. Commit only swaps the vnode and compacts pending fragment storage into the
    /// committed layout.
    pub(crate) fn commit_mount(&self, mount: MountId, node: &VNode) {
        let mut mounts = self.runtime.mounts.borrow_mut();
        let mount_state = &mut mounts[mount.0];
        mount_state.node = node.clone();
        compact_fragment_child_mounts(mount_state, node);
    }

    pub(crate) fn replace_mounted_component_root_mount(
        &self,
        old_root_mount: MountId,
        new_root_mount: MountId,
        render_parent: MountedParent,
    ) {
        self.runtime.mounts.borrow_mut()[new_root_mount.0].render_parent = Some(render_parent);

        if old_root_mount == new_root_mount {
            return;
        }

        for scope in self.runtime.scope_states.borrow().iter().flatten() {
            if scope.root_mount() == Some(old_root_mount) {
                scope.set_root_mount(Some(new_root_mount));
            }
        }
    }

    fn with_mount<R>(&self, mount: MountId, with_mount: impl FnOnce(&Mount) -> R) -> R {
        self.runtime
            .mounts
            .borrow()
            .get(mount.0)
            .map(with_mount)
            .expect("mount")
    }

    fn with_mount_mut<R>(&self, mount: MountId, with_mount: impl FnOnce(&mut Mount) -> R) -> R {
        self.runtime
            .mounts
            .borrow_mut()
            .get_mut(mount.0)
            .map(with_mount)
            .expect("mount")
    }
}

fn compact_fragment_child_mounts(mount: &mut Mount, node: &VNode) {
    if mount.fragment_child_mounts.is_empty() {
        return;
    }

    let old_children = std::mem::take(&mut mount.fragment_child_mounts);
    for anchor in node.dynamic_anchors() {
        for slot in anchor.nodes() {
            let idx = slot.index();
            let DynamicNode::Fragment(nodes) = &*slot else {
                continue;
            };

            if nodes.is_empty() {
                *mount.dynamic_slot_mut(idx) = PackedMountedSlot::empty();
                continue;
            }

            let start = mount
                .dynamic_slot(idx)
                .fragment_start()
                .expect("fragment children");
            let range = start..start + nodes.len();
            let new_start = mount.fragment_child_mounts.len();
            mount
                .fragment_child_mounts
                .extend_from_slice(&old_children[range]);
            *mount.dynamic_slot_mut(idx) =
                PackedMountedSlot::from_slot(MountedDynamicNodeSlot::Fragment(new_start));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::arena::ElementId;
    use std::mem::size_of;

    #[test]
    fn mounted_element_option_is_one_word() {
        assert_eq!(size_of::<Option<MountedElementId>>(), size_of::<usize>());
    }

    #[test]
    fn packed_dynamic_slot_is_one_word() {
        assert_eq!(size_of::<PackedMountedSlot>(), size_of::<usize>());
    }

    #[test]
    fn packed_dynamic_slot_round_trips() {
        let text = MountedDynamicNodeSlot::Text(MountedElementId::new(ElementId::new(7)));
        let component = MountedDynamicNodeSlot::Component(ScopeId::new(3));

        assert_eq!(
            PackedMountedSlot::from_slot(MountedDynamicNodeSlot::Empty),
            PackedMountedSlot::empty()
        );
        assert_eq!(
            PackedMountedSlot::from_slot(text).text(),
            Some(MountedElementId::new(ElementId::new(7)))
        );
        assert_eq!(
            PackedMountedSlot::from_slot(component).component_scope(),
            Some(ScopeId::new(3))
        );

        let fragment = PackedMountedSlot::from_slot(MountedDynamicNodeSlot::Fragment(17));
        assert_eq!(fragment.fragment_start(), Some(17));
    }
}

/// A retained suspense branch.
///
/// Suspense keeps the hidden primary branch alive while the fallback branch is
/// visible. The root `VNode` is the render output we diff, and the branch also
/// records the root mount identity so the boundary state is tied to retained
/// mount identity.
#[derive(Clone)]
pub(crate) struct SuspenseBranch {
    root: VNode,
    root_mount: MountId,
}

impl SuspenseBranch {
    pub(crate) fn new(root: VNode, root_mount: MountId) -> Self {
        Self { root, root_mount }
    }

    pub(crate) fn root(&self) -> VNode {
        self.root.clone()
    }

    pub(crate) fn mounted_root(&self) -> crate::MountedVNode<'_> {
        crate::MountedVNode::new(&self.root, self.root_mount)
    }

    pub(crate) fn root_mount(&self) -> MountId {
        self.root_mount
    }

    pub(crate) fn into_root(self) -> VNode {
        self.root
    }
}