Skip to content

Commit a76a81b

Browse files
committed
Remove style clone in grid algorithm
1 parent bd9c9ca commit a76a81b

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

src/compute/grid/mod.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
4040

4141
let get_child_styles_iter =
4242
|node| tree.child_ids(node).map(|child_node: NodeId| tree.get_grid_child_style(child_node));
43-
let style = tree.get_grid_container_style(node).clone();
43+
let style = tree.get_grid_container_style(node);
4444
let child_styles_iter = get_child_styles_iter(node);
4545

4646
let preferred_size = if inputs.sizing_mode == SizingMode::InherentSize {
@@ -130,6 +130,13 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
130130
content_box_inset.right += scrollbar_gutter.x;
131131
content_box_inset.bottom += scrollbar_gutter.y;
132132

133+
let align_content = style.align_content().unwrap_or(AlignContent::Stretch);
134+
let align_items = style.align_items();
135+
let justify_content = style.justify_content().unwrap_or(AlignContent::Stretch);
136+
let justify_items = style.justify_items();
137+
138+
drop(style);
139+
133140
let constrained_available_space = known_dimensions
134141
.or(size)
135142
.map(|size| size.map(AvailableSpace::Definite))
@@ -176,7 +183,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
176183
AbstractAxis::Inline,
177184
min_size.get(AbstractAxis::Inline),
178185
max_size.get(AbstractAxis::Inline),
179-
style.grid_align_content(AbstractAxis::Block),
186+
align_content,
180187
available_grid_space,
181188
inner_node_size,
182189
&mut columns,
@@ -196,7 +203,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
196203
AbstractAxis::Block,
197204
min_size.get(AbstractAxis::Block),
198205
max_size.get(AbstractAxis::Block),
199-
style.grid_align_content(AbstractAxis::Inline),
206+
justify_content,
200207
available_grid_space,
201208
inner_node_size,
202209
&mut rows,
@@ -309,7 +316,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
309316
AbstractAxis::Inline,
310317
min_size.get(AbstractAxis::Inline),
311318
max_size.get(AbstractAxis::Inline),
312-
style.grid_align_content(AbstractAxis::Block),
319+
align_content,
313320
available_grid_space,
314321
inner_node_size,
315322
&mut columns,
@@ -371,7 +378,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
371378
AbstractAxis::Block,
372379
min_size.get(AbstractAxis::Block),
373380
max_size.get(AbstractAxis::Block),
374-
style.grid_align_content(AbstractAxis::Inline),
381+
justify_content,
375382
available_grid_space,
376383
inner_node_size,
377384
&mut rows,
@@ -391,15 +398,15 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
391398
Line { start: padding.left, end: padding.right },
392399
Line { start: border.left, end: border.right },
393400
&mut columns,
394-
style.justify_content().unwrap_or(AlignContent::Stretch),
401+
justify_content,
395402
);
396403
// Align rows
397404
align_tracks(
398405
container_content_box.get(AbstractAxis::Block),
399406
Line { start: padding.top, end: padding.bottom },
400407
Line { start: border.top, end: border.bottom },
401408
&mut rows,
402-
style.align_content().unwrap_or(AlignContent::Stretch),
409+
align_content,
403410
);
404411

405412
// 9. Size, Align, and Position Grid Items
@@ -410,8 +417,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
410417
// Sort items back into original order to allow them to be matched up with styles
411418
items.sort_by_key(|item| item.source_order);
412419

413-
let container_alignment_styles = InBothAbsAxis { horizontal: style.justify_items(), vertical: style.align_items() };
414-
drop(style);
420+
let container_alignment_styles = InBothAbsAxis { horizontal: justify_items, vertical: align_items };
415421

416422
// Position in-flow children (stored in items vector)
417423
for (index, item) in items.iter_mut().enumerate() {

src/tree/taffy_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,11 @@ impl<'t, NodeContext, MeasureFunction> LayoutGridContainer for TaffyView<'t, Nod
372372
where
373373
MeasureFunction: FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>) -> Size<f32>,
374374
{
375-
type ContainerStyle = Style;
375+
type ContainerStyle<'a> = &'a Style where Self: 'a;
376376
type ItemStyle<'a> = &'a Style where Self: 'a;
377377

378378
#[inline(always)]
379-
fn get_grid_container_style(&self, node_id: NodeId) -> &Self::ContainerStyle {
379+
fn get_grid_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_> {
380380
&self.taffy.nodes[node_id.into()].style
381381
}
382382

src/tree/traits.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,17 @@ pub trait LayoutFlexboxContainer: LayoutPartialTree {
224224
/// Extends [`LayoutPartialTree`] with getters for the styles required for CSS Grid layout
225225
pub trait LayoutGridContainer: LayoutPartialTree {
226226
/// The style type representing the CSS Grid container's styles
227-
type ContainerStyle: GridContainerStyle + Clone;
227+
type ContainerStyle<'a>: GridContainerStyle
228+
where
229+
Self: 'a;
228230

229231
/// The style type representing each CSS Grid item's styles
230232
type ItemStyle<'a>: GridItemStyle
231233
where
232234
Self: 'a;
233235

234236
/// Get the container's styles
235-
fn get_grid_container_style(&self, node_id: NodeId) -> &Self::ContainerStyle;
237+
fn get_grid_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_>;
236238

237239
/// Get the child's styles
238240
fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::ItemStyle<'_>;

0 commit comments

Comments
 (0)