From fbff00341d67933f194ee8fd8ebbdd12bfa7fc4f Mon Sep 17 00:00:00 2001 From: Nor Khasyatillah Date: Mon, 19 Aug 2024 15:46:21 +0700 Subject: [PATCH] impl Clone for Box --- src/lib.rs | 21 ++++++++++++++++++++- tests/gradient.rs | 6 ++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9704480..249bbf6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,7 +150,7 @@ pub enum BlendMode { Lab, } -pub trait Gradient { +pub trait Gradient: CloneGradient { /// Get color at certain position fn at(&self, t: f32) -> Color; @@ -205,6 +205,25 @@ pub trait Gradient { } } +pub trait CloneGradient { + fn clone_gradient(&self) -> Box; +} + +impl CloneGradient for T +where + T: Gradient + Clone + 'static, +{ + fn clone_gradient(&self) -> Box { + Box::new(self.clone()) + } +} + +impl Clone for Box { + fn clone(&self) -> Self { + self.clone_gradient() + } +} + fn convert_colors(colors: &[Color], mode: BlendMode) -> Vec<[f32; 4]> { colors .iter() diff --git a/tests/gradient.rs b/tests/gradient.rs index cd55a70..67e49e3 100644 --- a/tests/gradient.rs +++ b/tests/gradient.rs @@ -138,3 +138,9 @@ fn colors() { &["#ff0000", "#808000", "#00ff00", "#008080", "#0000ff",] ); } + +#[test] +fn box_clone() { + let g: Box = Box::new(GradientBuilder::new().build::().unwrap()); + let _: Box = g.clone(); +}