Skip to content

Commit

Permalink
derive Eq trait
Browse files Browse the repository at this point in the history
derive applies the necessary trait bounds to derived impl.

see rust-lang/rust#21237 for more details
  • Loading branch information
michaelkirk committed Apr 1, 2020
1 parent a057c21 commit 302d17d
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 14 deletions.
32 changes: 29 additions & 3 deletions geo-types/src/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Eq>(pub T);
/// let _: AssertEq<Coordinate<i32>> = AssertEq(Coordinate { x: 1, y: 2 });
/// ```
///
/// ```compile_fail
/// use geo_types::Coordinate;
///
/// struct AssertEq<T: Eq>(pub T);
/// // Eq impl is not derived for Coordinate<f32> because f32 is not Eq
/// let _: AssertEq<Coordinate<f32>> = 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<T>
where
Expand All @@ -17,8 +45,6 @@ where
pub y: T,
}

impl<T: CoordinateType> std::cmp::Eq for Coordinate<T> where T: std::cmp::Eq {}

impl<T: CoordinateType> From<(T, T)> for Coordinate<T> {
fn from(coords: (T, T)) -> Self {
Coordinate {
Expand Down
32 changes: 31 additions & 1 deletion geo-types/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Eq>(pub T);
/// let _: AssertEq<Geometry<i32>> = AssertEq(Geometry::Point(Point::new(1, 2)));
/// ```
///
/// ```compile_fail
/// use geo_types::{Geometry, Point};
///
/// struct AssertEq<T: Eq>(pub T);
/// // Eq impl is not derived for Geometry<f32> because f32 is not Eq
/// let _: AssertEq<Geometry<f32>> = AssertEq(Geometry::Point(Point::new(1.0, 2.0)));
/// ```
#[derive(Eq, PartialEq, Clone, Debug, Hash)]
pub enum Geometry<T>
where
T: CoordinateType,
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/geometry_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(pub Vec<Geometry<T>>)
where
T: CoordinateType;
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/line.rs
Original file line number Diff line number Diff line change
@@ -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<T>
where
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(pub Vec<Coordinate<T>>)
where
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/multi_line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(pub Vec<LineString<T>>)
where
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/multi_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(pub Vec<Point<T>>)
where
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/multi_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(pub Vec<Polygon<T>>)
where
Expand Down
30 changes: 29 additions & 1 deletion geo-types/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,35 @@ use std::ops::Sub;
/// let c = Coordinate { x: 10., y: 20. };
/// let p2: Point<f64> = 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<T: Eq>(pub T);
/// let _: AssertEq<Point<i32>> = AssertEq(Point::new(1, 2));
/// ```
///
/// ```compile_fail
/// use geo_types::Point;
///
/// struct AssertEq<T: Eq>(pub T);
/// // Eq impl is not derived for Point<f32> because f32 is not Eq
/// let _: AssertEq<Point<f32>> = AssertEq(Point::new(1.0, 2.0));
/// ```
#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Point<T>(pub Coordinate<T>)
where
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
where
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/rect.rs
Original file line number Diff line number Diff line change
@@ -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<T>
where
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/triangle.rs
Original file line number Diff line number Diff line change
@@ -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<T: CoordinateType>(pub Coordinate<T>, pub Coordinate<T>, pub Coordinate<T>);

Expand Down

0 comments on commit 302d17d

Please sign in to comment.