From 2088e5d66117dd481e4c60ba6afe9ab8f3a2d4c1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 12 Mar 2024 13:25:06 +0100 Subject: Try using closures for `Container::style` `Box` should not allocate for zero-sized types; so we should not be incurring much overhead. Just a bit of indirection. --- widget/src/container.rs | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) (limited to 'widget/src/container.rs') diff --git a/widget/src/container.rs b/widget/src/container.rs index d606b0aa..f51ff878 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -36,7 +36,7 @@ pub struct Container< vertical_alignment: alignment::Vertical, clip: bool, content: Element<'a, Message, Theme, Renderer>, - style: Style, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Container<'a, Message, Theme, Renderer> @@ -48,15 +48,15 @@ where content: impl Into>, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { - Self::with_style(content, Theme::default_style()) + Self::with_style(content, Theme::default_style) } /// Creates a [`Container`] with the given content and style. pub fn with_style( content: impl Into>, - style: fn(&Theme, Status) -> Appearance, + style: impl Fn(&Theme, Status) -> Appearance + 'a, ) -> Self { let content = content.into(); let size = content.as_widget().size_hint(); @@ -71,8 +71,8 @@ where horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, clip: false, + style: Box::new(style), content, - style, } } @@ -137,8 +137,11 @@ where } /// Sets the style of the [`Container`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } @@ -546,41 +549,41 @@ pub enum Status { } /// The style of a [`Container`]. -pub type Style = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box Appearance + 'a>; /// The default style of a [`Container`]. pub trait DefaultStyle { /// Returns the default style of a [`Container`]. - fn default_style() -> Style; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style { - transparent + fn default_style(&self, status: Status) -> Appearance { + transparent(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } impl DefaultStyle for Color { - fn default_style() -> Style { - |color, _status| Appearance::default().with_background(*color) + fn default_style(&self, _status: Status) -> Appearance { + Appearance::default().with_background(*self) } } impl DefaultStyle for Gradient { - fn default_style() -> Style { - |gradient, _status| Appearance::default().with_background(*gradient) + fn default_style(&self, _status: Status) -> Appearance { + Appearance::default().with_background(*self) } } impl DefaultStyle for gradient::Linear { - fn default_style() -> Style { - |gradient, _status| Appearance::default().with_background(*gradient) + fn default_style(&self, _status: Status) -> Appearance { + Appearance::default().with_background(*self) } } -- cgit