Skip to content

Commit

Permalink
feat: add .push for Path and Paths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tirithen committed Jul 1, 2024
1 parent a0677f5 commit 3764676
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
11 changes: 9 additions & 2 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,27 @@ 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),
serde(bound = "P: PointScaler")
)]
pub struct Path<P: PointScaler = Centi>(Vec<Point<P>>);

impl<P: PointScaler> Eq for Path<P> {}

impl<P: PointScaler> Path<P> {
/// Create a new path from a vector of points.
pub fn new(points: Vec<Point<P>>) -> Self {
Path(points)
}

/// In place push point onto this path.
pub fn push(&mut self, point: impl Into<Point<P>>) {
self.0.push(point.into());
}

/// Returns the number of points in the path.
pub fn len(&self) -> usize {
self.0.len()
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -33,6 +33,13 @@ impl<P: PointScaler> Paths<P> {
Paths(paths)
}

/// In place push paths onto this set of paths.
pub fn push(&mut self, paths: impl Into<Paths<P>>) {
for path in paths.into() {
self.0.push(path);
}
}

/// Returns the number of paths.
pub fn len(&self) -> usize {
self.0.len()
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 7 additions & 13 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,31 +25,31 @@ 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 {
const MULTIPLIER: f64 = 1.0;
}

/// Scale by 10.
#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
pub struct Deci;

impl PointScaler for Deci {
const MULTIPLIER: f64 = 10.0;
}

/// 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 {
const MULTIPLIER: f64 = 100.0;
}

/// Scale by 1000.
#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
pub struct Milli;

impl PointScaler for Milli {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -214,12 +214,6 @@ impl<P: PointScaler> From<Point<P>> for [f64; 2] {
}
}

impl<P: PointScaler> PartialEq for Point<P> {
fn eq(&self, other: &Self) -> bool {
self.0.x == other.0.x && self.0.y == other.0.y
}
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -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 {
Expand Down

0 comments on commit 3764676

Please sign in to comment.