diff --git a/geo-types/CHANGES.md b/geo-types/CHANGES.md index e7f83ac55..e751f3a0a 100644 --- a/geo-types/CHANGES.md +++ b/geo-types/CHANGES.md @@ -3,6 +3,7 @@ ## Unreleased - Implement `RTreeObject` for `Triangle`. +- Implement `AsRef` for `Point` and `Coord`. ## 0.7.14 diff --git a/geo-types/src/geometry/coord.rs b/geo-types/src/geometry/coord.rs index 08ef00bb8..d34ece538 100644 --- a/geo-types/src/geometry/coord.rs +++ b/geo-types/src/geometry/coord.rs @@ -498,3 +498,9 @@ where } } } + +impl AsRef> for Coord { + fn as_ref(&self) -> &Coord { + self + } +} diff --git a/geo-types/src/geometry/point.rs b/geo-types/src/geometry/point.rs index 7b7c7f9fd..aa22a33cb 100644 --- a/geo-types/src/geometry/point.rs +++ b/geo-types/src/geometry/point.rs @@ -732,6 +732,12 @@ where } } +impl AsRef> for Point { + fn as_ref(&self) -> &Coord { + &self.0 + } +} + #[cfg(test)] mod test { use super::*; diff --git a/geo-types/src/geometry/triangle.rs b/geo-types/src/geometry/triangle.rs index 504919438..7993ff44b 100644 --- a/geo-types/src/geometry/triangle.rs +++ b/geo-types/src/geometry/triangle.rs @@ -1,4 +1,5 @@ use crate::{polygon, Coord, CoordNum, Line, Point, Polygon}; +use crate::{polygon, Coord, CoordNum, Line, Point, Polygon}; #[cfg(any(feature = "approx", test))] use approx::{AbsDiffEq, RelativeEq}; @@ -166,7 +167,7 @@ macro_rules! impl_rstar_triangle { fn envelope(&self) -> Self::Envelope { let bounding_rect = - crate::private_utils::get_bounding_rect(self.to_array().into_iter()).unwrap(); + crate::private_utils::get_bounding_rect(self.to_array()).unwrap(); ::$rstar::AABB::from_corners(bounding_rect.min().into(), bounding_rect.max().into()) } } diff --git a/geo-types/src/private_utils.rs b/geo-types/src/private_utils.rs index 138311618..001fcffbb 100644 --- a/geo-types/src/private_utils.rs +++ b/geo-types/src/private_utils.rs @@ -9,7 +9,7 @@ pub fn line_string_bounding_rect(line_string: &LineString) -> Option(line: Line) -> Rect @@ -19,17 +19,19 @@ where Rect::new(line.start, line.end) } -pub fn get_bounding_rect(collection: I) -> Option> +pub fn get_bounding_rect(collection: I) -> Option> where T: CoordNum, - I: IntoIterator>, + C: AsRef>, + I: IntoIterator, { let mut iter = collection.into_iter(); if let Some(pnt) = iter.next() { + let pnt = pnt.as_ref(); let mut xrange = (pnt.x, pnt.x); let mut yrange = (pnt.y, pnt.y); for pnt in iter { - let (px, py) = pnt.x_y(); + let (px, py) = pnt.as_ref().x_y(); xrange = get_min_max(px, xrange.0, xrange.1); yrange = get_min_max(py, yrange.0, yrange.1); } diff --git a/geo/src/algorithm/bounding_rect.rs b/geo/src/algorithm/bounding_rect.rs index 7ded02805..4587eafe5 100644 --- a/geo/src/algorithm/bounding_rect.rs +++ b/geo/src/algorithm/bounding_rect.rs @@ -65,7 +65,7 @@ where /// /// Return the BoundingRect for a MultiPoint fn bounding_rect(&self) -> Self::Output { - get_bounding_rect(self.0.iter().map(|p| p.0)) + get_bounding_rect(&self.0) } } @@ -102,7 +102,7 @@ where /// /// Return the BoundingRect for a MultiLineString fn bounding_rect(&self) -> Self::Output { - get_bounding_rect(self.iter().flat_map(|line| line.0.iter().cloned())) + get_bounding_rect(self.iter().flat_map(|line| &line.0)) } } @@ -116,7 +116,7 @@ where /// Return the BoundingRect for a Polygon fn bounding_rect(&self) -> Self::Output { let line = self.exterior(); - get_bounding_rect(line.0.iter().cloned()) + get_bounding_rect(&line.0) } } @@ -129,10 +129,7 @@ where /// /// Return the BoundingRect for a MultiPolygon fn bounding_rect(&self) -> Self::Output { - get_bounding_rect( - self.iter() - .flat_map(|poly| poly.exterior().0.iter().cloned()), - ) + get_bounding_rect(self.iter().flat_map(|poly| &poly.exterior().0)) } } @@ -143,7 +140,7 @@ where type Output = Rect; fn bounding_rect(&self) -> Self::Output { - get_bounding_rect(self.to_array().iter().cloned()).unwrap() + get_bounding_rect(self.to_array()).unwrap() } } diff --git a/geo/src/algorithm/monotone/mono_poly.rs b/geo/src/algorithm/monotone/mono_poly.rs index 07bbbb256..738142995 100644 --- a/geo/src/algorithm/monotone/mono_poly.rs +++ b/geo/src/algorithm/monotone/mono_poly.rs @@ -53,7 +53,7 @@ impl MonoPoly { debug_assert_eq!(top.0.first(), bot.0.first()); debug_assert_eq!(top.0.last(), bot.0.last()); debug_assert_ne!(top.0.first(), top.0.last()); - let bounds = get_bounding_rect(top.0.iter().chain(bot.0.iter()).cloned()).unwrap(); + let bounds = get_bounding_rect(top.0.iter().chain(bot.0.iter())).unwrap(); Self { top, bot, bounds } }