From 37646761f20c803e667f98f8ede7a68f49af6df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20S=C3=B6derstr=C3=B6m?= Date: Tue, 2 Jul 2024 01:29:59 +0200 Subject: [PATCH] feat: add .push for Path and Paths Adds .push method to Path and Paths structs that allows extending a Path or a Paths struct with more points or paths. Also add common derive macros PartialEq, Eq, Hash to structs. --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/path.rs | 11 +++++++++-- src/paths.rs | 11 +++++++++-- src/point.rs | 20 +++++++------------- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a12d20f..79e883d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,9 +83,9 @@ dependencies = [ [[package]] name = "clipper2c-sys" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94f103dc502f24d06fd9850e463fe7a2f505b7b40c087d2cb17046afb0413d45" +checksum = "3e02bffc6328ae8e6985a68fa19dbe908a25651f4dda31a5fe6bc33cbcbaace6" dependencies = [ "cc", "libc", diff --git a/Cargo.toml b/Cargo.toml index efa0eba..e335938 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ serde = ["dep:serde", "clipper2c-sys/serde"] [dependencies] libc = "0.2" -clipper2c-sys = "0.1.2" +clipper2c-sys = "0.1.3" thiserror = "1.0.59" serde = { version = "1", features = ["derive"], optional = true } diff --git a/src/path.rs b/src/path.rs index 5e547cb..c659d0e 100644 --- a/src/path.rs +++ b/src/path.rs @@ -19,7 +19,7 @@ use crate::{ /// let path_from_tuples: Path = vec![(0.0, 0.0), (5.0, 0.0), (5.0, 6.0), (0.0, 6.0)].into(); /// let path_from_slices: Path = vec![[0.0, 0.0], [5.0, 0.0], [5.0, 6.0], [0.0, 6.0]].into(); /// ``` -#[derive(Debug, Clone, Default, PartialEq)] +#[derive(Debug, Clone, Default, PartialEq, Hash)] #[cfg_attr( feature = "serde", derive(serde::Serialize, serde::Deserialize), @@ -27,12 +27,19 @@ use crate::{ )] pub struct Path(Vec>); +impl Eq for Path

{} + impl Path

{ /// Create a new path from a vector of points. pub fn new(points: Vec>) -> Self { Path(points) } + /// In place push point onto this path. + pub fn push(&mut self, point: impl Into>) { + self.0.push(point.into()); + } + /// Returns the number of points in the path. pub fn len(&self) -> usize { self.0.len() @@ -387,7 +394,7 @@ mod test { #[test] fn test_from_custom_scaler() { - #[derive(Debug, Copy, Clone)] + #[derive(Debug, Clone, Copy, PartialEq, Hash)] struct CustomScaler; impl PointScaler for CustomScaler { diff --git a/src/paths.rs b/src/paths.rs index 1ec6444..f5112dd 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -19,7 +19,7 @@ use crate::{ /// let paths_from_single_vec: Paths = vec![(0.0, 0.0), (5.0, 0.0), (5.0, 6.0), (0.0, 6.0)].into(); /// let paths_from_vec_of_vecs: Paths = vec![vec![(0.0, 0.0), (5.0, 0.0), (5.0, 6.0), (0.0, 6.0)]].into(); /// ``` -#[derive(Debug, Clone, Default, PartialEq)] +#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] #[cfg_attr( feature = "serde", derive(serde::Serialize, serde::Deserialize), @@ -33,6 +33,13 @@ impl Paths

{ Paths(paths) } + /// In place push paths onto this set of paths. + pub fn push(&mut self, paths: impl Into>) { + for path in paths.into() { + self.0.push(path); + } + } + /// Returns the number of paths. pub fn len(&self) -> usize { self.0.len() @@ -398,7 +405,7 @@ mod test { #[test] fn test_from_custom_scaler() { - #[derive(Debug, Copy, Clone)] + #[derive(Debug, Clone, Copy, PartialEq, Hash)] struct CustomScaler; impl PointScaler for CustomScaler { diff --git a/src/point.rs b/src/point.rs index bdf5eb2..747b647 100644 --- a/src/point.rs +++ b/src/point.rs @@ -8,7 +8,7 @@ use clipper2c_sys::ClipperPoint64; /// The default multiplier is `Centi`, and others are provided by the library, /// but if needed the user can create a custom scaler struct that implements /// `PointScaler`. -pub trait PointScaler: Clone + Copy { +pub trait PointScaler: Clone + Copy + PartialEq + std::hash::Hash { /// The point multiplier. This is set to a custom value when implementing /// the `PointScaler` trait. const MULTIPLIER: f64; @@ -25,7 +25,7 @@ pub trait PointScaler: Clone + Copy { } /// No scaling. -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Hash)] pub struct One; impl PointScaler for One { @@ -33,7 +33,7 @@ impl PointScaler for One { } /// Scale by 10. -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Hash)] pub struct Deci; impl PointScaler for Deci { @@ -41,7 +41,7 @@ impl PointScaler for Deci { } /// Scale by 100. This is the default. -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Hash)] pub struct Centi; impl PointScaler for Centi { @@ -49,7 +49,7 @@ impl PointScaler for Centi { } /// Scale by 1000. -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Hash)] pub struct Milli; impl PointScaler for Milli { @@ -84,7 +84,7 @@ impl PointScaler for Milli { /// assert_eq!(point.x_scaled(), 1000); /// assert_eq!(point.y_scaled(), 2000); /// ``` -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[cfg_attr( feature = "serde", derive(serde::Serialize, serde::Deserialize), @@ -214,12 +214,6 @@ impl From> for [f64; 2] { } } -impl PartialEq for Point

{ - fn eq(&self, other: &Self) -> bool { - self.0.x == other.0.x && self.0.y == other.0.y - } -} - #[cfg(test)] mod test { use super::*; @@ -235,7 +229,7 @@ mod test { #[test] fn test_point_custom_scaler() { - #[derive(Debug, Copy, Clone)] + #[derive(Debug, Clone, Copy, PartialEq, Hash)] struct CustomScaler; impl PointScaler for CustomScaler {