Skip to content

Commit

Permalink
Override clone_from implementations for a few types
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed May 16, 2024
1 parent 1207aca commit 0f874cf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ impl<'a> Iterator for Frames<'a> {
}

/// A single animation frame
#[derive(Clone)]
pub struct Frame {
/// Delay between the frames in milliseconds
delay: Delay,
Expand All @@ -44,6 +43,24 @@ pub struct Frame {
buffer: RgbaImage,
}

impl Clone for Frame {
fn clone(&self) -> Self {
Self {
delay: self.delay,
left: self.left,
top: self.top,
buffer: self.buffer.clone(),
}
}

fn clone_from(&mut self, source: &Self) {
self.delay = source.delay;
self.left = source.left;
self.top = source.top;
self.buffer.clone_from(&source.buffer);
}
}

/// The delay of a frame relative to the previous one.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd)]
pub struct Delay {
Expand Down
6 changes: 6 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,12 @@ where
_phantom: PhantomData,
}
}

fn clone_from(&mut self, source: &Self) {
self.data.clone_from(&source.data);
self.width = source.width;
self.height = source.height;
}
}

impl<P, Container> GenericImageView for ImageBuffer<P, Container>
Expand Down
35 changes: 34 additions & 1 deletion src/dynimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use crate::{Rgb32FImage, Rgba32FImage};
/// would hardly be feasible as a simple enum, due to the sheer number of combinations of channel
/// kinds, channel order, and bit depth. Rather, this type provides an opinionated selection with
/// normalized channel order which can store common pixel values without loss.
#[derive(Clone, Debug, PartialEq)]
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum DynamicImage {
/// Each pixel in this image is 8-bit Luma
Expand Down Expand Up @@ -78,6 +78,39 @@ pub enum DynamicImage {
ImageRgba32F(Rgba32FImage),
}

impl Clone for DynamicImage {
fn clone(&self) -> Self {
match self {
Self::ImageLuma8(p) => Self::ImageLuma8(p.clone()),
Self::ImageLumaA8(p) => Self::ImageLumaA8(p.clone()),
Self::ImageRgb8(p) => Self::ImageRgb8(p.clone()),
Self::ImageRgba8(p) => Self::ImageRgba8(p.clone()),
Self::ImageLuma16(p) => Self::ImageLuma16(p.clone()),
Self::ImageLumaA16(p) => Self::ImageLumaA16(p.clone()),
Self::ImageRgb16(p) => Self::ImageRgb16(p.clone()),
Self::ImageRgba16(p) => Self::ImageRgba16(p.clone()),
Self::ImageRgb32F(p) => Self::ImageRgb32F(p.clone()),
Self::ImageRgba32F(p) => Self::ImageRgba32F(p.clone()),
}
}

fn clone_from(&mut self, source: &Self) {
match (self, source) {
(Self::ImageLuma8(p1), Self::ImageLuma8(p2)) => p1.clone_from(p2),
(Self::ImageLumaA8(p1), Self::ImageLumaA8(p2)) => p1.clone_from(p2),
(Self::ImageRgb8(p1), Self::ImageRgb8(p2)) => p1.clone_from(p2),
(Self::ImageRgba8(p1), Self::ImageRgba8(p2)) => p1.clone_from(p2),
(Self::ImageLuma16(p1), Self::ImageLuma16(p2)) => p1.clone_from(p2),
(Self::ImageLumaA16(p1), Self::ImageLumaA16(p2)) => p1.clone_from(p2),
(Self::ImageRgb16(p1), Self::ImageRgb16(p2)) => p1.clone_from(p2),
(Self::ImageRgba16(p1), Self::ImageRgba16(p2)) => p1.clone_from(p2),
(Self::ImageRgb32F(p1), Self::ImageRgb32F(p2)) => p1.clone_from(p2),
(Self::ImageRgba32F(p1), Self::ImageRgba32F(p2)) => p1.clone_from(p2),
(this, source) => *this = source.clone(),
}
}
}

macro_rules! dynamic_map(
($dynimage: expr, $image: pat => $action: expr) => ({
use DynamicImage::*;
Expand Down

0 comments on commit 0f874cf

Please sign in to comment.