Skip to content

Commit

Permalink
Require Default on all num types
Browse files Browse the repository at this point in the history
Also use CoordFloat instead of Float in rstar
This PR requires simultaneous modification to the proj repo.

See #746
  • Loading branch information
nyurik committed Mar 22, 2022
1 parent 3554f0d commit 1b2e965
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 19 deletions.
7 changes: 5 additions & 2 deletions geo-types/src/coordinate.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -303,7 +306,7 @@ where
#[cfg(feature = "rstar_0_8")]
impl<T> ::rstar_0_8::Point for Coordinate<T>
where
T: ::num_traits::Float + ::rstar_0_8::RTreeNum,
T: CoordFloat + ::rstar_0_8::RTreeNum,
{
type Scalar = T;

Expand Down Expand Up @@ -336,7 +339,7 @@ where
#[cfg(feature = "rstar_0_9")]
impl<T> ::rstar_0_9::Point for Coordinate<T>
where
T: ::num_traits::Float + ::rstar_0_9::RTreeNum,
T: CoordFloat + ::rstar_0_9::RTreeNum,
{
type Scalar = T;

Expand Down
10 changes: 1 addition & 9 deletions geo-types/src/geometry_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: CoordNum>(pub Vec<Geometry<T>>);

// Implementing Default by hand because T does not have Default restriction
// todo: consider adding Default as a CoordNum requirement
impl<T: CoordNum> Default for GeometryCollection<T> {
fn default() -> Self {
Self(Vec::new())
}
}

impl<T: CoordNum> GeometryCollection<T> {
/// Return an empty GeometryCollection
pub fn new() -> Self {
Expand Down
4 changes: 2 additions & 2 deletions geo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ impl<T: Num + Copy + NumCast + PartialOrd + Debug> 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<T: CoordinateType + Debug> CoordNum for T {}
impl<T: CoordinateType + Debug + Default> CoordNum for T {}

pub trait CoordFloat: CoordNum + Float {}
impl<T: CoordNum + Float> CoordFloat for T {}
Expand Down
6 changes: 4 additions & 2 deletions geo-types/src/line.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -226,7 +228,7 @@ macro_rules! impl_rstar_line {
($rstar:ident) => {
impl<T> ::$rstar::RTreeObject for Line<T>
where
T: ::num_traits::Float + ::$rstar::RTreeNum,
T: CoordFloat + ::$rstar::RTreeNum,
{
type Envelope = ::$rstar::AABB<Point<T>>;

Expand All @@ -238,7 +240,7 @@ macro_rules! impl_rstar_line {

impl<T> ::$rstar::PointDistance for Line<T>
where
T: ::num_traits::Float + ::$rstar::RTreeNum,
T: CoordFloat + ::$rstar::RTreeNum,
{
fn distance_2(&self, point: &Point<T>) -> T {
let d = crate::private_utils::point_line_euclidean_distance(*point, *self);
Expand Down
6 changes: 4 additions & 2 deletions geo-types/src/line_string.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -483,7 +485,7 @@ macro_rules! impl_rstar_line_string {
($rstar:ident) => {
impl<T> ::$rstar::RTreeObject for LineString<T>
where
T: ::num_traits::Float + ::$rstar::RTreeNum,
T: CoordFloat + ::$rstar::RTreeNum,
{
type Envelope = ::$rstar::AABB<Point<T>>;

Expand All @@ -505,7 +507,7 @@ macro_rules! impl_rstar_line_string {

impl<T> ::$rstar::PointDistance for LineString<T>
where
T: ::num_traits::Float + ::$rstar::RTreeNum,
T: CoordFloat + ::$rstar::RTreeNum,
{
fn distance_2(&self, point: &Point<T>) -> T {
let d = crate::private_utils::point_line_string_euclidean_distance(*point, self);
Expand Down
4 changes: 2 additions & 2 deletions geo-types/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ where
// These are required for rstar RTree
impl<T> ::rstar_0_8::Point for Point<T>
where
T: ::num_traits::Float + ::rstar_0_8::RTreeNum,
T: CoordFloat + ::rstar_0_8::RTreeNum,
{
type Scalar = T;

Expand Down Expand Up @@ -580,7 +580,7 @@ where
#[cfg(feature = "rstar_0_9")]
impl<T> ::rstar_0_9::Point for Point<T>
where
T: ::num_traits::Float + ::rstar_0_9::RTreeNum,
T: CoordFloat + ::rstar_0_9::RTreeNum,
{
type Scalar = T;

Expand Down
1 change: 1 addition & 0 deletions geo/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
* <https://github.com/georust/geo/pull/757>
* BREAKING: Add `Default` constraint to all `CoordNum`/`CoordFloat` values.

## 0.19.0

Expand Down

0 comments on commit 1b2e965

Please sign in to comment.