Skip to content

Commit

Permalink
impl Clone for Box<dyn Gradient>
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Aug 19, 2024
1 parent 684fa07 commit fbff003
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -205,6 +205,25 @@ pub trait Gradient {
}
}

pub trait CloneGradient {
fn clone_gradient(&self) -> Box<dyn Gradient>;
}

impl<T> CloneGradient for T
where
T: Gradient + Clone + 'static,
{
fn clone_gradient(&self) -> Box<dyn Gradient> {
Box::new(self.clone())
}
}

impl Clone for Box<dyn Gradient> {
fn clone(&self) -> Self {
self.clone_gradient()
}
}

fn convert_colors(colors: &[Color], mode: BlendMode) -> Vec<[f32; 4]> {
colors
.iter()
Expand Down
6 changes: 6 additions & 0 deletions tests/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,9 @@ fn colors() {
&["#ff0000", "#808000", "#00ff00", "#008080", "#0000ff",]
);
}

#[test]
fn box_clone() {
let g: Box<dyn Gradient> = Box::new(GradientBuilder::new().build::<LinearGradient>().unwrap());
let _: Box<dyn Gradient> = g.clone();
}

0 comments on commit fbff003

Please sign in to comment.