Skip to content

Commit

Permalink
Add fallible in-place map trait
Browse files Browse the repository at this point in the history
  • Loading branch information
urschrei committed Mar 15, 2018
1 parent b8babce commit bc172f0
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions src/algorithm/map_coords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ pub trait MapCoordsInplace<T> {
T: CoordinateType;
}

/// Map a fallible function over the coordinates in a geometry, mutating them
pub trait MapCoordsInplaceFallible<T> {
/// Map a fallible function over the coordinates in a geometry, mutating them
///
/// ```
/// use geo::Point;
/// use geo::algorithm::map_coords::MapCoordsInplaceFallible;
///
/// let mut p = Point::new(10., 20.);
/// p.map_coords_inplace_fallible(&|&(x, y)| Ok((x+1000., y*2.))).unwrap();
///
/// assert_eq!(p, Point::new(1010., 40.));
/// ```
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error>
where
T: CoordinateType;
}

impl<T: CoordinateType, NT: CoordinateType> MapCoords<T, NT> for Point<T> {
type Output = Point<NT>;

Expand Down Expand Up @@ -106,6 +127,18 @@ impl<T: CoordinateType> MapCoordsInplace<T> for Point<T> {
}
}

impl<T: CoordinateType> MapCoordsInplaceFallible<T> for Point<T> {
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error> {
let new_point = func(&(self.0.x, self.0.y))?;
self.0.x = new_point.0;
self.0.y = new_point.1;
Ok(())
}
}

impl<T: CoordinateType, NT: CoordinateType> MapCoords<T, NT> for Line<T> {
type Output = Line<NT>;

Expand Down Expand Up @@ -135,6 +168,17 @@ impl<T: CoordinateType> MapCoordsInplace<T> for Line<T> {
}
}

impl<T: CoordinateType> MapCoordsInplaceFallible<T> for Line<T> {
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error> {
self.start.map_coords_inplace_fallible(func)?;
self.end.map_coords_inplace_fallible(func)?;
Ok(())
}
}

impl<T: CoordinateType, NT: CoordinateType> MapCoords<T, NT> for LineString<T> {
type Output = LineString<NT>;

Expand Down Expand Up @@ -165,6 +209,18 @@ impl<T: CoordinateType> MapCoordsInplace<T> for LineString<T> {
}
}

impl<T: CoordinateType> MapCoordsInplaceFallible<T> for LineString<T> {
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error> {
for p in self.0.iter_mut() {
p.map_coords_inplace_fallible(func)?;
}
Ok(())
}
}

impl<T: CoordinateType, NT: CoordinateType> MapCoords<T, NT> for Polygon<T> {
type Output = Polygon<NT>;

Expand Down Expand Up @@ -202,6 +258,19 @@ impl<T: CoordinateType> MapCoordsInplace<T> for Polygon<T> {
}
}

impl<T: CoordinateType> MapCoordsInplaceFallible<T> for Polygon<T> {
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error> {
self.exterior.map_coords_inplace_fallible(func)?;
for p in self.interiors.iter_mut() {
p.map_coords_inplace_fallible(func)?;
}
Ok(())
}
}

impl<T: CoordinateType, NT: CoordinateType> MapCoords<T, NT> for MultiPoint<T> {
type Output = MultiPoint<NT>;

Expand Down Expand Up @@ -232,6 +301,18 @@ impl<T: CoordinateType> MapCoordsInplace<T> for MultiPoint<T> {
}
}

impl<T: CoordinateType> MapCoordsInplaceFallible<T> for MultiPoint<T> {
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error> {
for p in self.0.iter_mut() {
p.map_coords_inplace_fallible(func)?;
}
Ok(())
}
}

impl<T: CoordinateType, NT: CoordinateType> MapCoords<T, NT> for MultiLineString<T> {
type Output = MultiLineString<NT>;

Expand Down Expand Up @@ -262,6 +343,18 @@ impl<T: CoordinateType> MapCoordsInplace<T> for MultiLineString<T> {
}
}

impl<T: CoordinateType> MapCoordsInplaceFallible<T> for MultiLineString<T> {
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error> {
for p in self.0.iter_mut() {
p.map_coords_inplace_fallible(func)?;
}
Ok(())
}
}

impl<T: CoordinateType, NT: CoordinateType> MapCoords<T, NT> for MultiPolygon<T> {
type Output = MultiPolygon<NT>;

Expand Down Expand Up @@ -292,6 +385,18 @@ impl<T: CoordinateType> MapCoordsInplace<T> for MultiPolygon<T> {
}
}

impl<T: CoordinateType> MapCoordsInplaceFallible<T> for MultiPolygon<T> {
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error> {
for p in self.0.iter_mut() {
p.map_coords_inplace_fallible(func)?;
}
Ok(())
}
}

impl<T: CoordinateType, NT: CoordinateType> MapCoords<T, NT> for Geometry<T> {
type Output = Geometry<NT>;

Expand Down Expand Up @@ -350,6 +455,24 @@ impl<T: CoordinateType> MapCoordsInplace<T> for Geometry<T> {
}
}

impl<T: CoordinateType> MapCoordsInplaceFallible<T> for Geometry<T> {
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error> {
match *self {
Geometry::Point(ref mut x) => x.map_coords_inplace_fallible(func),
Geometry::Line(ref mut x) => x.map_coords_inplace_fallible(func),
Geometry::LineString(ref mut x) => x.map_coords_inplace_fallible(func),
Geometry::Polygon(ref mut x) => x.map_coords_inplace_fallible(func),
Geometry::MultiPoint(ref mut x) => x.map_coords_inplace_fallible(func),
Geometry::MultiLineString(ref mut x) => x.map_coords_inplace_fallible(func),
Geometry::MultiPolygon(ref mut x) => x.map_coords_inplace_fallible(func),
Geometry::GeometryCollection(ref mut x) => x.map_coords_inplace_fallible(func),
}
}
}

impl<T: CoordinateType, NT: CoordinateType> MapCoords<T, NT> for GeometryCollection<T> {
type Output = GeometryCollection<NT>;

Expand Down Expand Up @@ -380,6 +503,18 @@ impl<T: CoordinateType> MapCoordsInplace<T> for GeometryCollection<T> {
}
}

impl<T: CoordinateType> MapCoordsInplaceFallible<T> for GeometryCollection<T> {
fn map_coords_inplace_fallible(
&mut self,
func: &Fn(&(T, T)) -> Result<(T, T), Error>,
) -> Result<(), Error> {
for p in self.0.iter_mut() {
p.map_coords_inplace_fallible(func)?;
}
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit bc172f0

Please sign in to comment.