Skip to content

Commit f953fcb

Browse files
authored
"Traitify" the Style struct (#568)
* Create style traits * Add algorithm-specific PartialLayoutTree traits * Taffy tree changes * Convert algorithms to traitified styles * Remove style clone in grid algorithm * Fixup convert * Make CacheMut an associated type * Update examples to use new traits * Fix --no-default-features * Fixup is_block * Fix clippy lints * Fix --no-default-features taffy_tree * Give style trait associated types unique names
1 parent 6fb38bf commit f953fcb

19 files changed

+1286
-374
lines changed

examples/custom_tree_owned_partial.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ impl taffy::TraversePartialTree for Node {
126126
}
127127

128128
impl taffy::LayoutPartialTree for Node {
129-
fn get_style(&self, node_id: NodeId) -> &Style {
129+
type CoreContainerStyle<'a> = &'a Style where
130+
Self: 'a;
131+
132+
type CacheMut<'b> = &'b mut Cache where Self: 'b;
133+
134+
fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
130135
&self.node_from_id(node_id).style
131136
}
132137

@@ -162,6 +167,38 @@ impl taffy::LayoutPartialTree for Node {
162167
}
163168
}
164169

170+
impl taffy::LayoutFlexboxContainer for Node {
171+
type FlexboxContainerStyle<'a> = &'a Style where
172+
Self: 'a;
173+
174+
type FlexboxItemStyle<'a> = &'a Style where
175+
Self: 'a;
176+
177+
fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
178+
&self.node_from_id(node_id).style
179+
}
180+
181+
fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
182+
&self.node_from_id(child_node_id).style
183+
}
184+
}
185+
186+
impl taffy::LayoutGridContainer for Node {
187+
type GridContainerStyle<'a> = &'a Style where
188+
Self: 'a;
189+
190+
type GridItemStyle<'a> = &'a Style where
191+
Self: 'a;
192+
193+
fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
194+
&self.node_from_id(node_id).style
195+
}
196+
197+
fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
198+
&self.node_from_id(child_node_id).style
199+
}
200+
}
201+
165202
fn main() -> Result<(), taffy::TaffyError> {
166203
let mut root = Node::new_column(Style::DEFAULT);
167204

examples/custom_tree_owned_unsafe.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ impl TraversePartialTree for StatelessLayoutTree {
130130
impl TraverseTree for StatelessLayoutTree {}
131131

132132
impl LayoutPartialTree for StatelessLayoutTree {
133-
fn get_style(&self, node_id: NodeId) -> &Style {
133+
type CoreContainerStyle<'a> = &'a Style where
134+
Self: 'a;
135+
136+
type CacheMut<'b> = &'b mut Cache where Self: 'b;
137+
138+
fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
134139
unsafe { &node_from_id(node_id).style }
135140
}
136141

@@ -166,6 +171,38 @@ impl LayoutPartialTree for StatelessLayoutTree {
166171
}
167172
}
168173

174+
impl taffy::LayoutFlexboxContainer for StatelessLayoutTree {
175+
type FlexboxContainerStyle<'a> = &'a Style where
176+
Self: 'a;
177+
178+
type FlexboxItemStyle<'a> = &'a Style where
179+
Self: 'a;
180+
181+
fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
182+
unsafe { &node_from_id(node_id).style }
183+
}
184+
185+
fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
186+
unsafe { &node_from_id(child_node_id).style }
187+
}
188+
}
189+
190+
impl taffy::LayoutGridContainer for StatelessLayoutTree {
191+
type GridContainerStyle<'a> = &'a Style where
192+
Self: 'a;
193+
194+
type GridItemStyle<'a> = &'a Style where
195+
Self: 'a;
196+
197+
fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
198+
unsafe { &node_from_id(node_id).style }
199+
}
200+
201+
fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
202+
unsafe { &node_from_id(child_node_id).style }
203+
}
204+
}
205+
169206
impl RoundTree for StatelessLayoutTree {
170207
fn get_unrounded_layout(&self, node_id: NodeId) -> &Layout {
171208
unsafe { &node_from_id_mut(node_id).unrounded_layout }

examples/custom_tree_vec.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ impl taffy::TraversePartialTree for Tree {
139139
impl taffy::TraverseTree for Tree {}
140140

141141
impl taffy::LayoutPartialTree for Tree {
142-
fn get_style(&self, node_id: NodeId) -> &Style {
142+
type CoreContainerStyle<'a> = &'a Style where
143+
Self: 'a;
144+
145+
type CacheMut<'b> = &'b mut Cache where Self: 'b;
146+
147+
fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
143148
&self.node_from_id(node_id).style
144149
}
145150

@@ -175,6 +180,38 @@ impl taffy::LayoutPartialTree for Tree {
175180
}
176181
}
177182

183+
impl taffy::LayoutFlexboxContainer for Tree {
184+
type FlexboxContainerStyle<'a> = &'a Style where
185+
Self: 'a;
186+
187+
type FlexboxItemStyle<'a> = &'a Style where
188+
Self: 'a;
189+
190+
fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
191+
&self.node_from_id(node_id).style
192+
}
193+
194+
fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
195+
&self.node_from_id(child_node_id).style
196+
}
197+
}
198+
199+
impl taffy::LayoutGridContainer for Tree {
200+
type GridContainerStyle<'a> = &'a Style where
201+
Self: 'a;
202+
203+
type GridItemStyle<'a> = &'a Style where
204+
Self: 'a;
205+
206+
fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
207+
&self.node_from_id(node_id).style
208+
}
209+
210+
fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
211+
&self.node_from_id(child_node_id).style
212+
}
213+
}
214+
178215
impl taffy::RoundTree for Tree {
179216
fn get_unrounded_layout(&self, node_id: NodeId) -> &Layout {
180217
&self.node_from_id(node_id).unrounded_layout

0 commit comments

Comments
 (0)