From 833538ee7f3a60a839304762dfc29b0881d19094 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:11:32 +0100 Subject: Leverage `DefaultStyle` traits instead of `Default` --- widget/src/svg.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'widget/src/svg.rs') diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 34fd9a7b..6e61d27a 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -32,14 +32,14 @@ impl Svg { /// Creates a new [`Svg`] from the given [`Handle`]. pub fn new(handle: impl Into) -> Self where - Style: Default, + Theme: DefaultStyle, { Svg { handle: handle.into(), width: Length::Fill, height: Length::Shrink, content_fit: ContentFit::Contain, - style: Style::default(), + style: Theme::default_style(), } } @@ -48,7 +48,7 @@ impl Svg { #[must_use] pub fn from_path(path: impl Into) -> Self where - Style: Default, + Theme: DefaultStyle, { Self::new(Handle::from_path(path)) } @@ -81,7 +81,7 @@ impl Svg { /// Sets the style variant of this [`Svg`]. #[must_use] pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } } @@ -163,7 +163,7 @@ where Status::Idle }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); renderer.draw( self.handle.clone(), @@ -214,25 +214,22 @@ pub struct Appearance { } /// The style of an [`Svg`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style(fn(&Theme, Status) -> Appearance); +pub type Style = fn(&Theme, Status) -> Appearance; -impl Clone for Style { - fn clone(&self) -> Self { - *self - } +/// The default style of an [`Svg`]. +pub trait DefaultStyle { + /// Returns the default style of an [`Svg`]. + fn default_style() -> Style; } -impl Copy for Style {} - -impl Default for Style { - fn default() -> Self { - Style(|_, _| Appearance::default()) +impl DefaultStyle for Theme { + fn default_style() -> Style { + |_theme, _status| Appearance::default() } } -impl From Appearance> for Style { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style { + |appearance, _status| *appearance } } -- cgit