From 7da55411693098c62bbb5faddfcd1a33f7660775 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 24 Mar 2025 16:42:14 -0400 Subject: [PATCH] rely on deriving traits instead --- src/agent.rs | 30 ++---------------------------- src/buffer.rs | 14 ++------------ src/imgdata.rs | 27 ++++----------------------- 3 files changed, 8 insertions(+), 63 deletions(-) diff --git a/src/agent.rs b/src/agent.rs index 0cf032a..53cf30d 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -6,7 +6,7 @@ use std::f32::consts::TAU; use std::fmt::{Display, Formatter}; // A single Physarum agent. The x and y positions are continuous, hence we use floating point numbers instead of integers. -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub struct Agent { pub x: f32, pub y: f32, @@ -17,11 +17,7 @@ pub struct Agent { impl Display for Agent { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{{\n(x,y): ({},{})\nangle: {}\npopulation id: {}\ni: {}}}", - self.x, self.y, self.angle, self.population_id, self.i, - ) + write!(f, "{:?}", self) } } @@ -93,25 +89,3 @@ impl Agent { self.y = wrap(self.y + step_distance * sin(self.angle), height as f32); } } - -impl Clone for Agent { - fn clone(&self) -> Agent { - Agent { - x: self.x, - y: self.y, - angle: self.angle, - population_id: self.population_id, - i: self.i, - } - } -} - -impl PartialEq for Agent { - fn eq(&self, other: &Self) -> bool { - self.x == other.x - && self.y == other.y - && self.angle == other.angle - && self.population_id == other.population_id - && self.i == other.i - } -} diff --git a/src/buffer.rs b/src/buffer.rs index b804ddd..2555248 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -1,22 +1,12 @@ -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Buf { pub width: usize, pub height: usize, pub buf: Vec, } -impl Clone for Buf { - fn clone(&self) -> Buf { - Buf { - width: self.width, - height: self.height, - buf: self.buf.clone(), - } - } -} - impl Buf { - pub fn new(width: usize, height: usize, buf: Vec) -> Self { + pub const fn new(width: usize, height: usize, buf: Vec) -> Self { Buf { width, height, buf } } diff --git a/src/imgdata.rs b/src/imgdata.rs index 0fd7ab2..c41d03c 100644 --- a/src/imgdata.rs +++ b/src/imgdata.rs @@ -2,23 +2,14 @@ use crate::{grid::Grid, palette::Palette}; use itertools::multizip; -// Stores data that is located in grids that is used for image generation +/// Stores data that is located in grids that is used for image generation +#[derive(Clone)] pub struct ThinGridData { pub width: usize, pub height: usize, pub data: Vec, } -impl Clone for ThinGridData { - fn clone(&self) -> ThinGridData { - ThinGridData { - width: self.width, - height: self.height, - data: self.data.clone(), - } - } -} - impl ThinGridData { // Convert Grid to ThinGridData pub fn new_from_grid(in_grid: &Grid) -> Self { @@ -69,23 +60,14 @@ impl ThinGridData { } } -// Class for storing data that will be used to create images +/// Class for storing data that will be used to create images +#[derive(Clone)] pub struct ImgData { pub grids: Vec, pub palette: Palette, pub iteration: i32, } -impl Clone for ImgData { - fn clone(&self) -> ImgData { - ImgData { - grids: self.grids.clone(), - palette: self.palette, - iteration: self.iteration, - } - } -} - impl ImgData { pub fn new(in_grids: Vec, in_palette: Palette, in_iteration: i32) -> Self { ImgData { @@ -105,7 +87,6 @@ impl ImgData { output } - #[inline] pub fn save_to_image(&self) { let (width, height) = (self.grids[0].width, self.grids[0].height); let mut img = image::RgbImage::new(width as u32, height as u32);