From 39bc4635f5d965c75f19f79960178726e6b73044 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 22 Feb 2022 01:40:03 -0500 Subject: [PATCH] Compact simple where clauses I think it gives relatively little value to use a verbose multiline `where T: CoordNum` form when it can be included in the impl declaration, e.g. `impl Line {` would be used instead of this long form: ``` impl Line where T: CoordNum, { ``` The more complex cases, as well as functions should keep the where clause separate as they are long enough as it is. --- geo-types/src/coordinate.rs | 30 +++----------- geo-types/src/geometry.rs | 5 +-- geo-types/src/geometry_collection.rs | 4 +- geo-types/src/line.rs | 10 +---- geo-types/src/line_string.rs | 4 +- geo-types/src/multi_line_string.rs | 4 +- geo-types/src/multi_point.rs | 4 +- geo-types/src/multi_polygon.rs | 4 +- geo-types/src/point.rs | 59 ++++++---------------------- geo-types/src/polygon.rs | 15 ++----- geo-types/src/rect.rs | 5 +-- 11 files changed, 30 insertions(+), 114 deletions(-) diff --git a/geo-types/src/coordinate.rs b/geo-types/src/coordinate.rs index 60d00b142e..47ddc61cc3 100644 --- a/geo-types/src/coordinate.rs +++ b/geo-types/src/coordinate.rs @@ -25,10 +25,7 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; /// [vector space]: //en.wikipedia.org/wiki/Vector_space #[derive(Eq, PartialEq, Clone, Copy, Debug, Hash, Default)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Coordinate -where - T: CoordNum, -{ +pub struct Coordinate { pub x: T, pub y: T, } @@ -72,10 +69,7 @@ impl From> for [T; 2] { } } -impl Coordinate -where - T: CoordNum, -{ +impl Coordinate { /// Returns a tuple that contains the x/horizontal & y/vertical component of the coordinate. /// /// # Examples @@ -137,10 +131,7 @@ where /// assert_eq!(sum.x, 2.75); /// assert_eq!(sum.y, 5.0); /// ``` -impl Add for Coordinate -where - T: CoordNum, -{ +impl Add for Coordinate { type Output = Coordinate; fn add(self, rhs: Coordinate) -> Coordinate { @@ -162,10 +153,7 @@ where /// assert_eq!(diff.x, 0.25); /// assert_eq!(diff.y, 0.); /// ``` -impl Sub for Coordinate -where - T: CoordNum, -{ +impl Sub for Coordinate { type Output = Coordinate; fn sub(self, rhs: Coordinate) -> Coordinate { @@ -186,10 +174,7 @@ where /// assert_eq!(q.x, 5.0); /// assert_eq!(q.y, 10.0); /// ``` -impl Mul for Coordinate -where - T: CoordNum, -{ +impl Mul for Coordinate { type Output = Coordinate; fn mul(self, rhs: T) -> Coordinate { @@ -210,10 +195,7 @@ where /// assert_eq!(q.x, 1.25); /// assert_eq!(q.y, 2.5); /// ``` -impl Div for Coordinate -where - T: CoordNum, -{ +impl Div for Coordinate { type Output = Coordinate; fn div(self, rhs: T) -> Coordinate { diff --git a/geo-types/src/geometry.rs b/geo-types/src/geometry.rs index f4aed8a9ca..330fd76629 100644 --- a/geo-types/src/geometry.rs +++ b/geo-types/src/geometry.rs @@ -26,10 +26,7 @@ use std::convert::TryFrom; /// #[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub enum Geometry -where - T: CoordNum, -{ +pub enum Geometry { Point(Point), Line(Line), LineString(LineString), diff --git a/geo-types/src/geometry_collection.rs b/geo-types/src/geometry_collection.rs index c66cb8ca89..810c662d55 100644 --- a/geo-types/src/geometry_collection.rs +++ b/geo-types/src/geometry_collection.rs @@ -71,9 +71,7 @@ use std::ops::{Index, IndexMut}; /// #[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct GeometryCollection(pub Vec>) -where - T: CoordNum; +pub struct GeometryCollection(pub Vec>); // Implementing Default by hand because T does not have Default restriction // todo: consider adding Default as a CoordNum requirement diff --git a/geo-types/src/line.rs b/geo-types/src/line.rs index c816846a2a..8c5e62c45e 100644 --- a/geo-types/src/line.rs +++ b/geo-types/src/line.rs @@ -11,18 +11,12 @@ use approx::{AbsDiffEq, RelativeEq}; /// `LineString` with the two end points. #[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Line -where - T: CoordNum, -{ +pub struct Line { pub start: Coordinate, pub end: Coordinate, } -impl Line -where - T: CoordNum, -{ +impl Line { /// Creates a new line segment. /// /// # Examples diff --git a/geo-types/src/line_string.rs b/geo-types/src/line_string.rs index b65dadeaf8..55f2f2c788 100644 --- a/geo-types/src/line_string.rs +++ b/geo-types/src/line_string.rs @@ -132,9 +132,7 @@ use std::ops::{Index, IndexMut}; #[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct LineString(pub Vec>) -where - T: CoordNum; +pub struct LineString(pub Vec>); /// A [`Point`] iterator returned by the `points` method #[derive(Debug)] diff --git a/geo-types/src/multi_line_string.rs b/geo-types/src/multi_line_string.rs index fa7422057a..9d4e7b77e2 100644 --- a/geo-types/src/multi_line_string.rs +++ b/geo-types/src/multi_line_string.rs @@ -33,9 +33,7 @@ use std::iter::FromIterator; /// of a closed `MultiLineString` is always empty. #[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct MultiLineString(pub Vec>) -where - T: CoordNum; +pub struct MultiLineString(pub Vec>); impl MultiLineString { /// True if the MultiLineString is empty or if all of its LineStrings are closed - see diff --git a/geo-types/src/multi_point.rs b/geo-types/src/multi_point.rs index f6875bfd95..88e944aedb 100644 --- a/geo-types/src/multi_point.rs +++ b/geo-types/src/multi_point.rs @@ -30,9 +30,7 @@ use std::iter::FromIterator; /// ``` #[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct MultiPoint(pub Vec>) -where - T: CoordNum; +pub struct MultiPoint(pub Vec>); impl>> From for MultiPoint { /// Convert a single `Point` (or something which can be converted to a `Point`) into a diff --git a/geo-types/src/multi_polygon.rs b/geo-types/src/multi_polygon.rs index f291dc08e5..253d28a16e 100644 --- a/geo-types/src/multi_polygon.rs +++ b/geo-types/src/multi_polygon.rs @@ -27,9 +27,7 @@ use std::iter::FromIterator; /// predicates that operate on it. #[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct MultiPolygon(pub Vec>) -where - T: CoordNum; +pub struct MultiPolygon(pub Vec>); impl>> From for MultiPolygon { fn from(x: IP) -> Self { diff --git a/geo-types/src/point.rs b/geo-types/src/point.rs index 328a3b9255..3ddffad525 100644 --- a/geo-types/src/point.rs +++ b/geo-types/src/point.rs @@ -28,9 +28,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssi /// ``` #[derive(Eq, PartialEq, Clone, Copy, Debug, Hash, Default)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Point(pub Coordinate) -where - T: CoordNum; +pub struct Point(pub Coordinate); impl From> for Point { fn from(x: Coordinate) -> Point { @@ -62,10 +60,7 @@ impl From> for [T; 2] { } } -impl Point -where - T: CoordNum, -{ +impl Point { /// Creates a new point. /// /// # Examples @@ -230,10 +225,7 @@ where } } -impl Point -where - T: CoordNum, -{ +impl Point { /// Returns the dot product of the two points: /// `dot = x1 * x2 + y1 * y2` /// @@ -274,10 +266,7 @@ where } } -impl Point -where - T: CoordFloat, -{ +impl Point { /// Converts the (x,y) components of Point to degrees /// /// # Example @@ -338,10 +327,7 @@ where } } -impl Add for Point -where - T: CoordNum, -{ +impl Add for Point { type Output = Point; /// Add a point to the given point. @@ -361,10 +347,7 @@ where } } -impl AddAssign for Point -where - T: CoordNum, -{ +impl AddAssign for Point { /// Add a point to the given point and assign it to the original point. /// /// # Examples @@ -383,10 +366,7 @@ where } } -impl Sub for Point -where - T: CoordNum, -{ +impl Sub for Point { type Output = Point; /// Subtract a point from the given point. @@ -406,10 +386,7 @@ where } } -impl SubAssign for Point -where - T: CoordNum, -{ +impl SubAssign for Point { /// Subtract a point from the given point and assign it to the original point. /// /// # Examples @@ -428,10 +405,7 @@ where } } -impl Mul for Point -where - T: CoordNum, -{ +impl Mul for Point { type Output = Point; /// Scaler multiplication of a point @@ -451,10 +425,7 @@ where } } -impl MulAssign for Point -where - T: CoordNum, -{ +impl MulAssign for Point { /// Scaler multiplication of a point in place /// /// # Examples @@ -473,10 +444,7 @@ where } } -impl Div for Point -where - T: CoordNum, -{ +impl Div for Point { type Output = Point; /// Scaler division of a point @@ -496,10 +464,7 @@ where } } -impl DivAssign for Point -where - T: CoordNum, -{ +impl DivAssign for Point { /// Scaler division of a point in place /// /// # Examples diff --git a/geo-types/src/polygon.rs b/geo-types/src/polygon.rs index c1bed2bd9e..3764fe8f45 100644 --- a/geo-types/src/polygon.rs +++ b/geo-types/src/polygon.rs @@ -66,18 +66,12 @@ use approx::{AbsDiffEq, RelativeEq}; /// [`LineString`]: line_string/struct.LineString.html #[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Polygon -where - T: CoordNum, -{ +pub struct Polygon { exterior: LineString, interiors: Vec>, } -impl Polygon -where - T: CoordNum, -{ +impl Polygon { /// Create a new `Polygon` with the provided exterior `LineString` ring and /// interior `LineString` rings. /// @@ -408,10 +402,7 @@ enum ListSign { Mixed, } -impl Polygon -where - T: CoordFloat + Signed, -{ +impl Polygon { /// Determine whether a Polygon is convex // For each consecutive pair of edges of the polygon (each triplet of points), // compute the z-component of the cross product of the vectors defined by the diff --git a/geo-types/src/rect.rs b/geo-types/src/rect.rs index 3f4acec496..a7b69523b0 100644 --- a/geo-types/src/rect.rs +++ b/geo-types/src/rect.rs @@ -39,10 +39,7 @@ use approx::{AbsDiffEq, RelativeEq}; /// ``` #[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Rect -where - T: CoordNum, -{ +pub struct Rect { min: Coordinate, max: Coordinate, }