Skip to content

Commit

Permalink
Update examples to use new traits
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jul 16, 2024
1 parent dd3007c commit 824a3fd
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
39 changes: 38 additions & 1 deletion examples/custom_tree_owned_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ impl taffy::TraversePartialTree for Node {
}

impl taffy::LayoutPartialTree for Node {
fn get_style(&self, node_id: NodeId) -> &Style {
type CoreContainerStyle<'a> = &'a Style where
Self: 'a;

type CacheMut<'b> = &'b mut Cache where Self: 'b;

fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
&self.node_from_id(node_id).style
}

Expand Down Expand Up @@ -162,6 +167,38 @@ impl taffy::LayoutPartialTree for Node {
}
}

impl taffy::LayoutFlexboxContainer for Node {
type ContainerStyle<'a> = &'a Style where
Self: 'a;

type ItemStyle<'a> = &'a Style where
Self: 'a;

fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_> {
&self.node_from_id(node_id).style
}

fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::ItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}

impl taffy::LayoutGridContainer for Node {
type ContainerStyle<'a> = &'a Style where
Self: 'a;

type ItemStyle<'a> = &'a Style where
Self: 'a;

fn get_grid_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_> {
&self.node_from_id(node_id).style
}

fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::ItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}

fn main() -> Result<(), taffy::TaffyError> {
let mut root = Node::new_column(Style::DEFAULT);

Expand Down
39 changes: 38 additions & 1 deletion examples/custom_tree_owned_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ impl TraversePartialTree for StatelessLayoutTree {
impl TraverseTree for StatelessLayoutTree {}

impl LayoutPartialTree for StatelessLayoutTree {
fn get_style(&self, node_id: NodeId) -> &Style {
type CoreContainerStyle<'a> = &'a Style where
Self: 'a;

type CacheMut<'b> = &'b mut Cache where Self: 'b;

fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
unsafe { &node_from_id(node_id).style }
}

Expand Down Expand Up @@ -166,6 +171,38 @@ impl LayoutPartialTree for StatelessLayoutTree {
}
}

impl taffy::LayoutFlexboxContainer for StatelessLayoutTree {
type ContainerStyle<'a> = &'a Style where
Self: 'a;

type ItemStyle<'a> = &'a Style where
Self: 'a;

fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_> {
unsafe { &node_from_id(node_id).style }
}

fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::ItemStyle<'_> {
unsafe { &node_from_id(child_node_id).style }
}
}

impl taffy::LayoutGridContainer for StatelessLayoutTree {
type ContainerStyle<'a> = &'a Style where
Self: 'a;

type ItemStyle<'a> = &'a Style where
Self: 'a;

fn get_grid_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_> {
unsafe { &node_from_id(node_id).style }
}

fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::ItemStyle<'_> {
unsafe { &node_from_id(child_node_id).style }
}
}

impl RoundTree for StatelessLayoutTree {
fn get_unrounded_layout(&self, node_id: NodeId) -> &Layout {
unsafe { &node_from_id_mut(node_id).unrounded_layout }
Expand Down
39 changes: 38 additions & 1 deletion examples/custom_tree_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ impl taffy::TraversePartialTree for Tree {
impl taffy::TraverseTree for Tree {}

impl taffy::LayoutPartialTree for Tree {
fn get_style(&self, node_id: NodeId) -> &Style {
type CoreContainerStyle<'a> = &'a Style where
Self: 'a;

type CacheMut<'b> = &'b mut Cache where Self: 'b;

fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
&self.node_from_id(node_id).style
}

Expand Down Expand Up @@ -175,6 +180,38 @@ impl taffy::LayoutPartialTree for Tree {
}
}

impl taffy::LayoutFlexboxContainer for Tree {
type ContainerStyle<'a> = &'a Style where
Self: 'a;

type ItemStyle<'a> = &'a Style where
Self: 'a;

fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_> {
&self.node_from_id(node_id).style
}

fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::ItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}

impl taffy::LayoutGridContainer for Tree {
type ContainerStyle<'a> = &'a Style where
Self: 'a;

type ItemStyle<'a> = &'a Style where
Self: 'a;

fn get_grid_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_> {
&self.node_from_id(node_id).style
}

fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::ItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}

impl taffy::RoundTree for Tree {
fn get_unrounded_layout(&self, node_id: NodeId) -> &Layout {
&self.node_from_id(node_id).unrounded_layout
Expand Down

0 comments on commit 824a3fd

Please sign in to comment.