Skip to main content

LayoutNode

Struct LayoutNode 

Source
#[non_exhaustive]
pub struct LayoutNode { pub style: Style, pub self_fragments: Vec<ItemFragment>, pub children: Vec<LayoutNode>, pub layout_boxes: LayoutBoxes, pub placements: Vec<FragmentPlacement>, /* private fields */ }
Expand description

A node in the layout tree.

A LayoutNode represents a single layout object and is responsible for:

  • Holding layout-related style information
  • Owning child layout nodes (structural hierarchy)
  • Providing inline fragment inputs
  • Storing layout results (box model and fragment placements)

§Fragment model

self_fragments represent the input fragments provided by the user. They are immutable logical units and are never reordered by the layout engine.

placements represent the layout result for fragments. Each entry corresponds 1:1 to self_fragments and stores relative placement information computed during layout.

Only inline-level nodes are expected to have non-empty self_fragments. For block-level or flex-level nodes, this vector should be empty.

§Layout invariants

  • The order of self_fragments is preserved during layout
  • placements.len() == self_fragments.len() after layout
  • placements are meaningful only after layout computation

This structure intentionally does not distinguish between inline, block, or flex nodes at the type level; their behavior is defined by Style::display.

§Results storage

Results of the layout process are stored directly within each LayoutNode, allowing for easy access and further processing after layout computation. This includes the computed BoxModel and fragment placements.

  • layout_boxes: The computed box model for this node after layout.
  • placements: The computed placements for each fragment in self_fragments.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§style: Style§self_fragments: Vec<ItemFragment>§children: Vec<LayoutNode>§layout_boxes: LayoutBoxes§placements: Vec<FragmentPlacement>

Implementations§

Source§

impl LayoutNode

Source

pub fn new(style: Style) -> Self

Examples found in repository?
examples/layout_flex_chain_bench.rs (lines 6-23)
5fn node(display: Display) -> LayoutNode {
6    LayoutNode::new(Style {
7        display,
8        spacing: Spacing {
9            margin_left: Length::Px(2.0),
10            margin_right: Length::Px(2.0),
11            padding_left: Length::Px(1.0),
12            padding_right: Length::Px(1.0),
13            border_left: Length::Px(1.0),
14            border_right: Length::Px(1.0),
15            ..Default::default()
16        },
17        size: SizeStyle {
18            min_width: Length::Px(0.0),
19            max_width: Length::Px(10_000.0),
20            ..Default::default()
21        },
22        ..Default::default()
23    })
24}
More examples
Hide additional examples
examples/layout_bench.rs (lines 5-37)
4fn make_node(is_flex: bool) -> LayoutNode {
5    LayoutNode::new(Style {
6        display: if is_flex {
7            Display::Flex {
8                flex_direction: FlexDirection::Row,
9            }
10        } else {
11            Display::Block
12        },
13        size: SizeStyle {
14            width: Length::Auto,
15            height: Length::Auto,
16            min_width: Length::Px(0.0),
17            max_width: Length::Px(10_000.0),
18            min_height: Length::Px(0.0),
19            max_height: Length::Px(10_000.0),
20        },
21        spacing: Spacing {
22            margin_left: Length::Px(2.0),
23            margin_right: Length::Px(2.0),
24            margin_top: Length::Px(1.0),
25            margin_bottom: Length::Px(1.0),
26            padding_left: Length::Px(1.0),
27            padding_right: Length::Px(1.0),
28            padding_top: Length::Px(1.0),
29            padding_bottom: Length::Px(1.0),
30            border_left: Length::Px(1.0),
31            border_right: Length::Px(1.0),
32            border_top: Length::Px(1.0),
33            border_bottom: Length::Px(1.0),
34            ..Default::default()
35        },
36        ..Default::default()
37    })
38}
Source

pub fn with_children(style: Style, children: Vec<LayoutNode>) -> Self

Source

pub fn set_fragments(&mut self, fragments: Vec<ItemFragment>)

Sets input fragments for this node.

This method defines the inline content owned by the node. Any previously computed fragment placements become invalid and must be recomputed during the next layout pass.

Trait Implementations§

Source§

impl Debug for LayoutNode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.