From e864536b31be0c3a561521dcb17552c43e16d4e2 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 30 Mar 2020 21:31:18 -0700 Subject: [PATCH] Add a `From` implementation to `FillStyle` and use it in `canvas` for brevity --- canvas/src/lib.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/canvas/src/lib.rs b/canvas/src/lib.rs index 167da7b1..f75946f0 100644 --- a/canvas/src/lib.rs +++ b/canvas/src/lib.rs @@ -155,13 +155,13 @@ impl CanvasRenderingContext2D { // Fill and stroke styles #[inline] - pub fn set_fill_style(&mut self, new_fill_style: FillStyle) { - self.current_state.fill_paint = new_fill_style.into_paint(); + pub fn set_fill_style(&mut self, new_fill_style: FS) where FS: Into { + self.current_state.fill_paint = new_fill_style.into().into_paint(); } #[inline] - pub fn set_stroke_style(&mut self, new_stroke_style: FillStyle) { - self.current_state.stroke_paint = new_stroke_style.into_paint(); + pub fn set_stroke_style(&mut self, new_stroke_style: FS) where FS: Into { + self.current_state.stroke_paint = new_stroke_style.into().into_paint(); } // Shadows @@ -710,3 +710,24 @@ impl Debug for Path2D { self.clone().into_outline().fmt(formatter) } } + +impl From for FillStyle { + #[inline] + fn from(color: ColorU) -> FillStyle { + FillStyle::Color(color) + } +} + +impl From for FillStyle { + #[inline] + fn from(gradient: Gradient) -> FillStyle { + FillStyle::Gradient(gradient) + } +} + +impl From for FillStyle { + #[inline] + fn from(pattern: Pattern) -> FillStyle { + FillStyle::Pattern(pattern) + } +}