From cc77f7b4f9560a048cdf27764b079622c130927a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20S=C3=B6derstr=C3=B6m?= Date: Wed, 29 Jan 2025 01:38:10 +0100 Subject: [PATCH] feat: impl Default for points and paths --- src/path.rs | 27 ++++++++++++++++++++++++++- src/paths.rs | 29 +++++++++++++++++++++++++++-- src/point.rs | 12 ++++++------ 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/path.rs b/src/path.rs index a5947a5..8af4a92 100644 --- a/src/path.rs +++ b/src/path.rs @@ -383,8 +383,33 @@ impl From> for Path

{ #[cfg(test)] mod test { + use crate::Deci; + use super::*; + #[test] + fn test_default() { + let path: Path = Path::default(); + assert_eq!(path.len(), 0); + } + + #[test] + fn test_default_deci_precision() { + let path = Path::::default(); + assert_eq!(path.len(), 0); + } + + #[test] + fn test_default_as_struct_field() { + #[derive(Default)] + struct Foo { + path: Path, + } + + let path = Foo::default(); + assert_eq!(path.path.len(), 0); + } + #[test] fn test_negative_inflate_removing_imploded_paths() { let path: Path = vec![(0.0, 0.0), (5.0, 0.0), (5.0, 6.0), (0.0, 6.0)].into(); @@ -405,7 +430,7 @@ mod test { #[test] fn test_from_custom_scaler() { - #[derive(Debug, Clone, Copy, PartialEq, Hash)] + #[derive(Debug, Default, Clone, Copy, PartialEq, Hash)] struct CustomScaler; impl PointScaler for CustomScaler { diff --git a/src/paths.rs b/src/paths.rs index f5112dd..318dc04 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, Eq, Hash)] +#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] #[cfg_attr( feature = "serde", derive(serde::Serialize, serde::Deserialize), @@ -386,8 +386,33 @@ impl From>> for Paths

{ #[cfg(test)] mod test { + use crate::Deci; + use super::*; + #[test] + fn test_default() { + let paths: Paths = Paths::default(); + assert_eq!(paths.len(), 0); + } + + #[test] + fn test_default_deci_precision() { + let paths = Paths::::default(); + assert_eq!(paths.len(), 0); + } + + #[test] + fn test_default_as_struct_field() { + #[derive(Default)] + struct Foo { + paths: Paths, + } + + let paths = Foo::default(); + assert_eq!(paths.paths.len(), 0); + } + #[test] fn test_from() { let paths = Paths::::from(vec![(0.4, 0.0), (5.0, 1.0)]); @@ -405,7 +430,7 @@ mod test { #[test] fn test_from_custom_scaler() { - #[derive(Debug, Clone, Copy, PartialEq, Hash)] + #[derive(Debug, Default, Clone, Copy, PartialEq, Hash)] struct CustomScaler; impl PointScaler for CustomScaler { diff --git a/src/point.rs b/src/point.rs index 747b647..0889abc 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 + PartialEq + std::hash::Hash { +pub trait PointScaler: Default + 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 + PartialEq + std::hash::Hash { } /// No scaling. -#[derive(Debug, Copy, Clone, PartialEq, Hash)] +#[derive(Debug, Default, 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, Hash)] +#[derive(Debug, Default, 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, Hash)] +#[derive(Debug, Default, 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, Hash)] +#[derive(Debug, Default, Copy, Clone, PartialEq, Hash)] pub struct Milli; impl PointScaler for Milli { @@ -229,7 +229,7 @@ mod test { #[test] fn test_point_custom_scaler() { - #[derive(Debug, Clone, Copy, PartialEq, Hash)] + #[derive(Debug, Default, Clone, Copy, PartialEq, Hash)] struct CustomScaler; impl PointScaler for CustomScaler {