From 302d17da033a1b50c97e7b186a659db2102d5536 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 1 Apr 2020 14:45:51 -0600 Subject: [PATCH 1/2] derive Eq trait derive applies the necessary trait bounds to derived impl. see https://github.com/rust-lang/rust/pull/21237 for more details --- geo-types/src/coordinate.rs | 32 +++++++++++++++++++++++++--- geo-types/src/geometry.rs | 32 +++++++++++++++++++++++++++- geo-types/src/geometry_collection.rs | 2 +- geo-types/src/line.rs | 2 +- geo-types/src/line_string.rs | 2 +- geo-types/src/multi_line_string.rs | 2 +- geo-types/src/multi_point.rs | 2 +- geo-types/src/multi_polygon.rs | 2 +- geo-types/src/point.rs | 30 +++++++++++++++++++++++++- geo-types/src/polygon.rs | 2 +- geo-types/src/rect.rs | 2 +- geo-types/src/triangle.rs | 2 +- 12 files changed, 98 insertions(+), 14 deletions(-) diff --git a/geo-types/src/coordinate.rs b/geo-types/src/coordinate.rs index 4f6ecb12e..0e85c1236 100644 --- a/geo-types/src/coordinate.rs +++ b/geo-types/src/coordinate.rs @@ -7,7 +7,35 @@ use crate::{CoordinateType, Point}; /// as an envelope, a precision model, and spatial reference system /// information), a `Coordinate` only contains ordinate values and accessor /// methods. -#[derive(PartialEq, Clone, Copy, Debug, Hash)] +/// +/// ## Equality Examples +/// +/// ``` +/// use geo_types::Coordinate; +/// +/// let c1 = Coordinate { x: 1.0, y: 2.0}; +/// let c2 = Coordinate { x: 1.0, y: 2.0} ; +/// assert_eq!(c1, c2); +/// +/// let c3 = Coordinate { x: 1.0, y: 2.1}; +/// assert_ne!(c1, c3); +/// ``` +/// +/// ``` +/// use geo_types::Coordinate; +/// +/// struct AssertEq(pub T); +/// let _: AssertEq> = AssertEq(Coordinate { x: 1, y: 2 }); +/// ``` +/// +/// ```compile_fail +/// use geo_types::Coordinate; +/// +/// struct AssertEq(pub T); +/// // Eq impl is not derived for Coordinate because f32 is not Eq +/// let _: AssertEq> = AssertEq(Coordinate { x: 1.0, y: 2.0 }); +/// ``` +#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Coordinate where @@ -17,8 +45,6 @@ where pub y: T, } -impl std::cmp::Eq for Coordinate where T: std::cmp::Eq {} - impl From<(T, T)> for Coordinate { fn from(coords: (T, T)) -> Self { Coordinate { diff --git a/geo-types/src/geometry.rs b/geo-types/src/geometry.rs index 4f4ac4abf..452d4b3c9 100644 --- a/geo-types/src/geometry.rs +++ b/geo-types/src/geometry.rs @@ -23,7 +23,37 @@ use std::fmt; /// let pn = Point::try_from(pe).unwrap(); /// ``` /// -#[derive(PartialEq, Clone, Debug, Hash)] +/// ## Equality Examples +/// +/// ``` +/// use geo_types::{Geometry, Point}; +/// +/// let p1 = Point::new(1.0, 2.0); +/// let p2 = Point::new(1.0, 2.0); +/// let g1 = Geometry::Point(p1); +/// let g2 = Geometry::Point(p2); +/// assert_eq!(g1, g2); +/// +/// let p3 = Point::new(1.0, 2.1); +/// let g3 = Geometry::Point(p3); +/// assert_ne!(g1, g3); +/// ``` +/// +/// ``` +/// use geo_types::{Geometry, Point}; +/// +/// struct AssertEq(pub T); +/// let _: AssertEq> = AssertEq(Geometry::Point(Point::new(1, 2))); +/// ``` +/// +/// ```compile_fail +/// use geo_types::{Geometry, Point}; +/// +/// struct AssertEq(pub T); +/// // Eq impl is not derived for Geometry because f32 is not Eq +/// let _: AssertEq> = AssertEq(Geometry::Point(Point::new(1.0, 2.0))); +/// ``` +#[derive(Eq, PartialEq, Clone, Debug, Hash)] pub enum Geometry where T: CoordinateType, diff --git a/geo-types/src/geometry_collection.rs b/geo-types/src/geometry_collection.rs index 679e7f1e2..4613725de 100644 --- a/geo-types/src/geometry_collection.rs +++ b/geo-types/src/geometry_collection.rs @@ -63,7 +63,7 @@ use std::ops::{Index, IndexMut}; /// println!("{:?}", gc[0]); /// ``` /// -#[derive(PartialEq, Clone, Debug, Hash)] +#[derive(Eq, PartialEq, Clone, Debug, Hash)] pub struct GeometryCollection(pub Vec>) where T: CoordinateType; diff --git a/geo-types/src/line.rs b/geo-types/src/line.rs index bc16ed513..0395f1b14 100644 --- a/geo-types/src/line.rs +++ b/geo-types/src/line.rs @@ -1,7 +1,7 @@ use crate::{Coordinate, CoordinateType, Point}; /// A line segment made up of exactly two [`Point`s](struct.Point.html). -#[derive(PartialEq, Clone, Copy, Debug, Hash)] +#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Line where diff --git a/geo-types/src/line_string.rs b/geo-types/src/line_string.rs index feb0fe0db..406fb3476 100644 --- a/geo-types/src/line_string.rs +++ b/geo-types/src/line_string.rs @@ -73,7 +73,7 @@ use std::ops::{Index, IndexMut}; /// } /// ``` -#[derive(PartialEq, Clone, Debug, Hash)] +#[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct LineString(pub Vec>) where diff --git a/geo-types/src/multi_line_string.rs b/geo-types/src/multi_line_string.rs index a2334af15..c323cf340 100644 --- a/geo-types/src/multi_line_string.rs +++ b/geo-types/src/multi_line_string.rs @@ -6,7 +6,7 @@ use std::iter::FromIterator; /// Can be created from a `Vec` of `LineString`s, or from an Iterator which yields `LineString`s. /// /// Iterating over this objects, yields the component `LineString`s. -#[derive(PartialEq, Clone, Debug, Hash)] +#[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct MultiLineString(pub Vec>) where diff --git a/geo-types/src/multi_point.rs b/geo-types/src/multi_point.rs index 3056b9ed2..ea54fc34d 100644 --- a/geo-types/src/multi_point.rs +++ b/geo-types/src/multi_point.rs @@ -14,7 +14,7 @@ use std::iter::FromIterator; /// println!("Point x = {}, y = {}", point.x(), point.y()); /// } /// ``` -#[derive(PartialEq, Clone, Debug, Hash)] +#[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct MultiPoint(pub Vec>) where diff --git a/geo-types/src/multi_polygon.rs b/geo-types/src/multi_polygon.rs index caf548f0e..b0b14fad1 100644 --- a/geo-types/src/multi_polygon.rs +++ b/geo-types/src/multi_polygon.rs @@ -6,7 +6,7 @@ use std::iter::FromIterator; /// Can be created from a `Vec` of `Polygon`s, or `collect`ed from an Iterator which yields `Polygon`s. /// /// Iterating over this object yields the component Polygons. -#[derive(PartialEq, Clone, Debug, Hash)] +#[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct MultiPolygon(pub Vec>) where diff --git a/geo-types/src/point.rs b/geo-types/src/point.rs index d4c1ce9fd..ebaefcbeb 100644 --- a/geo-types/src/point.rs +++ b/geo-types/src/point.rs @@ -17,7 +17,35 @@ use std::ops::Sub; /// let c = Coordinate { x: 10., y: 20. }; /// let p2: Point = c.into(); /// ``` -#[derive(PartialEq, Clone, Copy, Debug, Hash)] +/// +/// ## Equality Examples +/// +/// ``` +/// use geo_types::Point; +/// +/// let p1 = Point::new(1.0, 2.0); +/// let p2 = Point::new(1.0, 2.0); +/// assert_eq!(p1, p2); +/// +/// let p3 = Point::new(1.0, 2.1); +/// assert_ne!(p1, p3); +/// ``` +/// +/// ``` +/// use geo_types::Point; +/// +/// struct AssertEq(pub T); +/// let _: AssertEq> = AssertEq(Point::new(1, 2)); +/// ``` +/// +/// ```compile_fail +/// use geo_types::Point; +/// +/// struct AssertEq(pub T); +/// // Eq impl is not derived for Point because f32 is not Eq +/// let _: AssertEq> = AssertEq(Point::new(1.0, 2.0)); +/// ``` +#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Point(pub Coordinate) where diff --git a/geo-types/src/polygon.rs b/geo-types/src/polygon.rs index 9b9403814..cdb8a0707 100644 --- a/geo-types/src/polygon.rs +++ b/geo-types/src/polygon.rs @@ -31,7 +31,7 @@ use num_traits::{Float, Signed}; /// the first `Coordinate`. /// /// [`LineString`]: line_string/struct.LineString.html -#[derive(PartialEq, Clone, Debug, Hash)] +#[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Polygon where diff --git a/geo-types/src/rect.rs b/geo-types/src/rect.rs index ea76295ef..2fa8a0f6a 100644 --- a/geo-types/src/rect.rs +++ b/geo-types/src/rect.rs @@ -1,7 +1,7 @@ use crate::{polygon, Coordinate, CoordinateType, Polygon}; /// A bounded 2D quadrilateral whose area is defined by minimum and maximum `Coordinates`. -#[derive(PartialEq, Clone, Copy, Debug, Hash)] +#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Rect where diff --git a/geo-types/src/triangle.rs b/geo-types/src/triangle.rs index e2efaa9e5..2304e9c89 100644 --- a/geo-types/src/triangle.rs +++ b/geo-types/src/triangle.rs @@ -1,7 +1,7 @@ use crate::{polygon, Coordinate, CoordinateType, Line, Polygon}; /// A bounded 2D area whose three vertices are defined by `Coordinate`s. -#[derive(Copy, Clone, Debug, Hash, PartialEq)] +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Triangle(pub Coordinate, pub Coordinate, pub Coordinate); From fc33f9fa8da2e3ade6ff0b047d467da91b41ca7e Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 1 Apr 2020 15:03:55 -0600 Subject: [PATCH 2/2] remove superfluous tests These tests are just testing how rust features works, not anything in this lib. --- geo-types/src/coordinate.rs | 28 ---------------------------- geo-types/src/geometry.rs | 30 ------------------------------ geo-types/src/point.rs | 28 ---------------------------- 3 files changed, 86 deletions(-) diff --git a/geo-types/src/coordinate.rs b/geo-types/src/coordinate.rs index 0e85c1236..dbc12f9c5 100644 --- a/geo-types/src/coordinate.rs +++ b/geo-types/src/coordinate.rs @@ -7,34 +7,6 @@ use crate::{CoordinateType, Point}; /// as an envelope, a precision model, and spatial reference system /// information), a `Coordinate` only contains ordinate values and accessor /// methods. -/// -/// ## Equality Examples -/// -/// ``` -/// use geo_types::Coordinate; -/// -/// let c1 = Coordinate { x: 1.0, y: 2.0}; -/// let c2 = Coordinate { x: 1.0, y: 2.0} ; -/// assert_eq!(c1, c2); -/// -/// let c3 = Coordinate { x: 1.0, y: 2.1}; -/// assert_ne!(c1, c3); -/// ``` -/// -/// ``` -/// use geo_types::Coordinate; -/// -/// struct AssertEq(pub T); -/// let _: AssertEq> = AssertEq(Coordinate { x: 1, y: 2 }); -/// ``` -/// -/// ```compile_fail -/// use geo_types::Coordinate; -/// -/// struct AssertEq(pub T); -/// // Eq impl is not derived for Coordinate because f32 is not Eq -/// let _: AssertEq> = AssertEq(Coordinate { x: 1.0, y: 2.0 }); -/// ``` #[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Coordinate diff --git a/geo-types/src/geometry.rs b/geo-types/src/geometry.rs index 452d4b3c9..a69870f8f 100644 --- a/geo-types/src/geometry.rs +++ b/geo-types/src/geometry.rs @@ -23,36 +23,6 @@ use std::fmt; /// let pn = Point::try_from(pe).unwrap(); /// ``` /// -/// ## Equality Examples -/// -/// ``` -/// use geo_types::{Geometry, Point}; -/// -/// let p1 = Point::new(1.0, 2.0); -/// let p2 = Point::new(1.0, 2.0); -/// let g1 = Geometry::Point(p1); -/// let g2 = Geometry::Point(p2); -/// assert_eq!(g1, g2); -/// -/// let p3 = Point::new(1.0, 2.1); -/// let g3 = Geometry::Point(p3); -/// assert_ne!(g1, g3); -/// ``` -/// -/// ``` -/// use geo_types::{Geometry, Point}; -/// -/// struct AssertEq(pub T); -/// let _: AssertEq> = AssertEq(Geometry::Point(Point::new(1, 2))); -/// ``` -/// -/// ```compile_fail -/// use geo_types::{Geometry, Point}; -/// -/// struct AssertEq(pub T); -/// // Eq impl is not derived for Geometry because f32 is not Eq -/// let _: AssertEq> = AssertEq(Geometry::Point(Point::new(1.0, 2.0))); -/// ``` #[derive(Eq, PartialEq, Clone, Debug, Hash)] pub enum Geometry where diff --git a/geo-types/src/point.rs b/geo-types/src/point.rs index ebaefcbeb..3fc3dae22 100644 --- a/geo-types/src/point.rs +++ b/geo-types/src/point.rs @@ -17,34 +17,6 @@ use std::ops::Sub; /// let c = Coordinate { x: 10., y: 20. }; /// let p2: Point = c.into(); /// ``` -/// -/// ## Equality Examples -/// -/// ``` -/// use geo_types::Point; -/// -/// let p1 = Point::new(1.0, 2.0); -/// let p2 = Point::new(1.0, 2.0); -/// assert_eq!(p1, p2); -/// -/// let p3 = Point::new(1.0, 2.1); -/// assert_ne!(p1, p3); -/// ``` -/// -/// ``` -/// use geo_types::Point; -/// -/// struct AssertEq(pub T); -/// let _: AssertEq> = AssertEq(Point::new(1, 2)); -/// ``` -/// -/// ```compile_fail -/// use geo_types::Point; -/// -/// struct AssertEq(pub T); -/// // Eq impl is not derived for Point because f32 is not Eq -/// let _: AssertEq> = AssertEq(Point::new(1.0, 2.0)); -/// ``` #[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Point(pub Coordinate)