Skip to main content

dioxus_core/view/
fragment.rs

1//! The typed fragment view: an ordered group of children with no enclosing
2//! element.
3
4use crate::DynamicValues;
5use dioxus_core_template::TemplateRawTree;
6
7use super::{IntoViewChild, View, ViewTemplate};
8
9/// A typed fragment view: an ordered group of children with no enclosing element.
10///
11/// Like [`ElementBuilder`](super::ElementBuilder) it collects children into a cons list via
12/// [`FragmentBuilder::child`], but it contributes no node of its own - it lowers to exactly its
13/// children, in order. That makes it the container for a template's roots and for grouping more
14/// siblings than a single tuple can hold: `View`/`ViewTemplate` are only implemented for tuples up
15/// to arity 128, so wider lists are split into several tuple groups joined through `.child(..)`.
16/// Nested fragments and tuples flatten transparently into the surrounding template, so the grouping
17/// leaves no trace in the lowered ops or dynamic-slot order.
18pub struct FragmentBuilder<Children> {
19    children: Children,
20}
21
22/// Create an empty typed fragment.
23#[inline]
24pub const fn fragment() -> FragmentBuilder<()> {
25    FragmentBuilder { children: () }
26}
27
28impl<Children> FragmentBuilder<Children> {
29    /// Append one child.
30    #[inline]
31    pub fn child<Child, Marker>(
32        self,
33        child: Child,
34    ) -> FragmentBuilder<(Children, <Child as IntoViewChild<Marker>>::Output)>
35    where
36        Child: IntoViewChild<Marker>,
37    {
38        FragmentBuilder {
39            children: (self.children, child.into_child()),
40        }
41    }
42}
43
44impl<Children: ViewTemplate> ViewTemplate for FragmentBuilder<Children> {
45    const TEMPLATE_TREE: &'static TemplateRawTree = Children::TEMPLATE_TREE;
46}
47
48impl<Children: View> View for FragmentBuilder<Children> {
49    #[inline]
50    fn push(self, dynamic: &mut DynamicValues) {
51        self.children.push(dynamic);
52    }
53}