gpui: Pass Style by value to request_layout (#11597)

Piotr Osiewicz created

A minor thing I've spotted and decided to fix on the spot.
It was being cloned twice within the body of that function (one of which
was redundant even without this PR); now in most cases we go down from 2
clones to 0.
Release Notes:

- N/A

Change summary

crates/editor/src/element.rs                 |  4 ++--
crates/gpui/src/element.rs                   |  2 +-
crates/gpui/src/elements/anchored.rs         |  2 +-
crates/gpui/src/elements/canvas.rs           |  2 +-
crates/gpui/src/elements/div.rs              |  2 +-
crates/gpui/src/elements/img.rs              |  2 +-
crates/gpui/src/elements/list.rs             |  2 +-
crates/gpui/src/elements/svg.rs              |  2 +-
crates/gpui/src/taffy.rs                     | 12 +++++-------
crates/gpui/src/view.rs                      |  2 +-
crates/gpui/src/window.rs                    |  2 +-
crates/markdown/src/markdown.rs              |  2 +-
crates/terminal_view/src/terminal_element.rs |  2 +-
crates/ui/src/components/popover_menu.rs     |  2 +-
crates/ui/src/components/right_click_menu.rs |  2 +-
crates/workspace/src/pane_group.rs           |  2 +-
16 files changed, 21 insertions(+), 23 deletions(-)

Detailed changes

crates/editor/src/element.rs 🔗

@@ -3661,7 +3661,7 @@ impl Element for EditorElement {
                     let mut style = Style::default();
                     style.size.width = relative(1.).into();
                     style.size.height = self.style.text.line_height_in_pixels(rem_size).into();
-                    cx.request_layout(&style, None)
+                    cx.request_layout(style, None)
                 }
                 EditorMode::AutoHeight { max_lines } => {
                     let editor_handle = cx.view().clone();
@@ -3685,7 +3685,7 @@ impl Element for EditorElement {
                     let mut style = Style::default();
                     style.size.width = relative(1.).into();
                     style.size.height = relative(1.).into();
-                    cx.request_layout(&style, None)
+                    cx.request_layout(style, None)
                 }
             };
 

crates/gpui/src/element.rs 🔗

@@ -603,7 +603,7 @@ impl Element for Empty {
         _id: Option<&GlobalElementId>,
         cx: &mut WindowContext,
     ) -> (LayoutId, Self::RequestLayoutState) {
-        (cx.request_layout(&Style::default(), None), ())
+        (cx.request_layout(Style::default(), None), ())
     }
 
     fn prepaint(

crates/gpui/src/elements/anchored.rs 🔗

@@ -93,7 +93,7 @@ impl Element for Anchored {
             ..Style::default()
         };
 
-        let layout_id = cx.request_layout(&anchored_style, child_layout_ids.iter().copied());
+        let layout_id = cx.request_layout(anchored_style, child_layout_ids.iter().copied());
 
         (layout_id, AnchoredState { child_layout_ids })
     }

crates/gpui/src/elements/canvas.rs 🔗

@@ -49,7 +49,7 @@ impl<T: 'static> Element for Canvas<T> {
     ) -> (crate::LayoutId, Self::RequestLayoutState) {
         let mut style = Style::default();
         style.refine(&self.style);
-        let layout_id = cx.request_layout(&style, []);
+        let layout_id = cx.request_layout(style.clone(), []);
         (layout_id, style)
     }
 

crates/gpui/src/elements/div.rs 🔗

@@ -1139,7 +1139,7 @@ impl Element for Div {
                         .iter_mut()
                         .map(|child| child.request_layout(cx))
                         .collect::<SmallVec<_>>();
-                    cx.request_layout(&style, child_layout_ids.iter().copied())
+                    cx.request_layout(style, child_layout_ids.iter().copied())
                 })
             });
         (layout_id, DivFrameState { child_layout_ids })

crates/gpui/src/elements/list.rs 🔗

@@ -766,7 +766,7 @@ impl Element for List {
                 let mut style = Style::default();
                 style.refine(&self.style);
                 cx.with_text_style(style.text_style().cloned(), |cx| {
-                    cx.request_layout(&style, None)
+                    cx.request_layout(style, None)
                 })
             }
         };

crates/gpui/src/elements/svg.rs 🔗

@@ -51,7 +51,7 @@ impl Element for Svg {
     ) -> (LayoutId, Self::RequestLayoutState) {
         let layout_id = self
             .interactivity
-            .request_layout(global_id, cx, |style, cx| cx.request_layout(&style, None));
+            .request_layout(global_id, cx, |style, cx| cx.request_layout(style, None));
         (layout_id, ())
     }
 

crates/gpui/src/taffy.rs 🔗

@@ -49,7 +49,7 @@ impl TaffyLayoutEngine {
 
     pub fn request_layout(
         &mut self,
-        style: &Style,
+        style: Style,
         rem_size: Pixels,
         children: &[LayoutId],
     ) -> LayoutId {
@@ -66,12 +66,11 @@ impl TaffyLayoutEngine {
                 .new_with_children(taffy_style, unsafe { std::mem::transmute(children) })
                 .expect(EXPECT_MESSAGE)
                 .into();
-            for child_id in children {
-                self.children_to_parents.insert(*child_id, parent_id);
-            }
+            self.children_to_parents
+                .extend(children.into_iter().map(|child_id| (*child_id, parent_id)));
             parent_id
         };
-        self.styles.insert(layout_id, style.clone());
+        self.styles.insert(layout_id, style);
         layout_id
     }
 
@@ -82,7 +81,6 @@ impl TaffyLayoutEngine {
         measure: impl FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>
             + 'static,
     ) -> LayoutId {
-        let style = style.clone();
         let taffy_style = style.to_taffy(rem_size);
 
         let layout_id = self
@@ -91,7 +89,7 @@ impl TaffyLayoutEngine {
             .expect(EXPECT_MESSAGE)
             .into();
         self.nodes_to_measure.insert(layout_id, Box::new(measure));
-        self.styles.insert(layout_id, style.clone());
+        self.styles.insert(layout_id, style);
         layout_id
     }
 

crates/gpui/src/view.rs 🔗

@@ -296,7 +296,7 @@ impl Element for AnyView {
         if let Some(style) = self.cached_style.as_ref() {
             let mut root_style = Style::default();
             root_style.refine(style);
-            let layout_id = cx.request_layout(&root_style, None);
+            let layout_id = cx.request_layout(root_style, None);
             (layout_id, None)
         } else {
             let mut element = (self.render)(self, cx);

crates/gpui/src/window.rs 🔗

@@ -2503,7 +2503,7 @@ impl<'a> WindowContext<'a> {
     /// This method should only be called as part of the request_layout or prepaint phase of element drawing.
     pub fn request_layout(
         &mut self,
-        style: &Style,
+        style: Style,
         children: impl IntoIterator<Item = LayoutId>,
     ) -> LayoutId {
         debug_assert_eq!(

crates/markdown/src/markdown.rs 🔗

@@ -568,7 +568,7 @@ impl Element for MarkdownElement {
 
         let mut rendered_markdown = builder.build();
         let child_layout_id = rendered_markdown.element.request_layout(cx);
-        let layout_id = cx.request_layout(&Style::default(), [child_layout_id]);
+        let layout_id = cx.request_layout(Style::default(), [child_layout_id]);
         (layout_id, rendered_markdown)
     }
 

crates/terminal_view/src/terminal_element.rs 🔗

@@ -559,7 +559,7 @@ impl Element for TerminalElement {
             .request_layout(global_id, cx, |mut style, cx| {
                 style.size.width = relative(1.).into();
                 style.size.height = relative(1.).into();
-                let layout_id = cx.request_layout(&style, None);
+                let layout_id = cx.request_layout(style, None);
 
                 layout_id
             });

crates/ui/src/components/popover_menu.rs 🔗

@@ -195,7 +195,7 @@ impl<M: ManagedView> Element for PopoverMenu<M> {
                     .map(|child_element| child_element.request_layout(cx));
 
                 let layout_id = cx.request_layout(
-                    &gpui::Style::default(),
+                    gpui::Style::default(),
                     menu_layout_id.into_iter().chain(child_layout_id),
                 );
 

crates/ui/src/components/right_click_menu.rs 🔗

@@ -142,7 +142,7 @@ impl<M: ManagedView> Element for RightClickMenu<M> {
                 .map(|child_element| child_element.request_layout(cx));
 
             let layout_id = cx.request_layout(
-                &gpui::Style::default(),
+                gpui::Style::default(),
                 menu_layout_id.into_iter().chain(child_layout_id),
             );
 

crates/workspace/src/pane_group.rs 🔗

@@ -810,7 +810,7 @@ mod element {
             style.flex_basis = relative(0.).into();
             style.size.width = relative(1.).into();
             style.size.height = relative(1.).into();
-            (cx.request_layout(&style, None), ())
+            (cx.request_layout(style, None), ())
         }
 
         fn prepaint(