From 1b2e965dad869aa2ed1062e95c00a11e75b48294 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 22 Feb 2022 17:05:22 -0500 Subject: [PATCH] Require Default on all num types Also use CoordFloat instead of Float in rstar This PR requires simultaneous modification to the proj repo. See https://github.com/georust/geo/discussions/746 --- geo-types/src/coordinate.rs | 7 +++++-- geo-types/src/geometry_collection.rs | 10 +--------- geo-types/src/lib.rs | 4 ++-- geo-types/src/line.rs | 6 ++++-- geo-types/src/line_string.rs | 6 ++++-- geo-types/src/point.rs | 4 ++-- geo/CHANGES.md | 1 + 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/geo-types/src/coordinate.rs b/geo-types/src/coordinate.rs index 9b720d910c..a5df8c612f 100644 --- a/geo-types/src/coordinate.rs +++ b/geo-types/src/coordinate.rs @@ -1,5 +1,8 @@ use crate::{coord, CoordNum, Point}; +#[cfg(any(feature = "rstar_0_8", feature = "rstar_0_9"))] +use crate::CoordFloat; + #[cfg(any(feature = "approx", test))] use approx::{AbsDiffEq, RelativeEq, UlpsEq}; @@ -303,7 +306,7 @@ where #[cfg(feature = "rstar_0_8")] impl ::rstar_0_8::Point for Coordinate where - T: ::num_traits::Float + ::rstar_0_8::RTreeNum, + T: CoordFloat + ::rstar_0_8::RTreeNum, { type Scalar = T; @@ -336,7 +339,7 @@ where #[cfg(feature = "rstar_0_9")] impl ::rstar_0_9::Point for Coordinate where - T: ::num_traits::Float + ::rstar_0_9::RTreeNum, + T: CoordFloat + ::rstar_0_9::RTreeNum, { type Scalar = T; diff --git a/geo-types/src/geometry_collection.rs b/geo-types/src/geometry_collection.rs index da022b8096..59557c246d 100644 --- a/geo-types/src/geometry_collection.rs +++ b/geo-types/src/geometry_collection.rs @@ -69,18 +69,10 @@ use std::ops::{Index, IndexMut}; /// println!("{:?}", gc[0]); /// ``` /// -#[derive(Eq, PartialEq, Clone, Debug, Hash)] +#[derive(Eq, PartialEq, Clone, Debug, Hash, Default)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct GeometryCollection(pub Vec>); -// Implementing Default by hand because T does not have Default restriction -// todo: consider adding Default as a CoordNum requirement -impl Default for GeometryCollection { - fn default() -> Self { - Self(Vec::new()) - } -} - impl GeometryCollection { /// Return an empty GeometryCollection pub fn new() -> Self { diff --git a/geo-types/src/lib.rs b/geo-types/src/lib.rs index 546e68360b..55a9b1343c 100644 --- a/geo-types/src/lib.rs +++ b/geo-types/src/lib.rs @@ -76,9 +76,9 @@ impl CoordinateType for T {} /// For algorithms which only make sense for floating point, like area or length calculations, /// see [CoordFloat](trait.CoordFloat.html). #[allow(deprecated)] -pub trait CoordNum: CoordinateType + Debug {} +pub trait CoordNum: CoordinateType + Debug + Default {} #[allow(deprecated)] -impl CoordNum for T {} +impl CoordNum for T {} pub trait CoordFloat: CoordNum + Float {} impl CoordFloat for T {} diff --git a/geo-types/src/line.rs b/geo-types/src/line.rs index fbee02a9d3..00b19608f3 100644 --- a/geo-types/src/line.rs +++ b/geo-types/src/line.rs @@ -1,3 +1,5 @@ +#[cfg(any(feature = "rstar_0_8", feature = "rstar_0_9"))] +use crate::CoordFloat; use crate::{CoordNum, Coordinate, Point}; #[cfg(any(feature = "approx", test))] use approx::{AbsDiffEq, RelativeEq}; @@ -226,7 +228,7 @@ macro_rules! impl_rstar_line { ($rstar:ident) => { impl ::$rstar::RTreeObject for Line where - T: ::num_traits::Float + ::$rstar::RTreeNum, + T: CoordFloat + ::$rstar::RTreeNum, { type Envelope = ::$rstar::AABB>; @@ -238,7 +240,7 @@ macro_rules! impl_rstar_line { impl ::$rstar::PointDistance for Line where - T: ::num_traits::Float + ::$rstar::RTreeNum, + T: CoordFloat + ::$rstar::RTreeNum, { fn distance_2(&self, point: &Point) -> T { let d = crate::private_utils::point_line_euclidean_distance(*point, *self); diff --git a/geo-types/src/line_string.rs b/geo-types/src/line_string.rs index c8a477be03..9771af61e2 100644 --- a/geo-types/src/line_string.rs +++ b/geo-types/src/line_string.rs @@ -1,3 +1,5 @@ +#[cfg(any(feature = "rstar_0_8", feature = "rstar_0_9"))] +use crate::CoordFloat; #[cfg(any(feature = "approx", test))] use approx::{AbsDiffEq, RelativeEq}; @@ -483,7 +485,7 @@ macro_rules! impl_rstar_line_string { ($rstar:ident) => { impl ::$rstar::RTreeObject for LineString where - T: ::num_traits::Float + ::$rstar::RTreeNum, + T: CoordFloat + ::$rstar::RTreeNum, { type Envelope = ::$rstar::AABB>; @@ -505,7 +507,7 @@ macro_rules! impl_rstar_line_string { impl ::$rstar::PointDistance for LineString where - T: ::num_traits::Float + ::$rstar::RTreeNum, + T: CoordFloat + ::$rstar::RTreeNum, { fn distance_2(&self, point: &Point) -> T { let d = crate::private_utils::point_line_string_euclidean_distance(*point, self); diff --git a/geo-types/src/point.rs b/geo-types/src/point.rs index cb704d2a92..0e7db45982 100644 --- a/geo-types/src/point.rs +++ b/geo-types/src/point.rs @@ -551,7 +551,7 @@ where // These are required for rstar RTree impl ::rstar_0_8::Point for Point where - T: ::num_traits::Float + ::rstar_0_8::RTreeNum, + T: CoordFloat + ::rstar_0_8::RTreeNum, { type Scalar = T; @@ -580,7 +580,7 @@ where #[cfg(feature = "rstar_0_9")] impl ::rstar_0_9::Point for Point where - T: ::num_traits::Float + ::rstar_0_9::RTreeNum, + T: CoordFloat + ::rstar_0_9::RTreeNum, { type Scalar = T; diff --git a/geo/CHANGES.md b/geo/CHANGES.md index 5caa1dc97b..76c819d937 100644 --- a/geo/CHANGES.md +++ b/geo/CHANGES.md @@ -5,6 +5,7 @@ * Add `LinesIter` algorithm to iterate over the lines in geometries. * Very similar to `CoordsIter`, but only implemented where it makes sense (e.g., for `Polygon`, `Rect`, but not `Point`). * +* BREAKING: Add `Default` constraint to all `CoordNum`/`CoordFloat` values. ## 0.19.0