Skip to content

Commit

Permalink
a few more generics
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Mar 19, 2022
1 parent 3046276 commit aef15ab
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 53 deletions.
10 changes: 5 additions & 5 deletions geo-types/src/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ impl<T: CoordNum, Z: ZCoord, M: Measure> From<PointTZM<T, Z, M>> for CoordTZM<T,
}
}

impl<T: CoordNum> From<Coordinate<T>> for (T, T) {
fn from(coord: Coordinate<T>) -> Self {
impl<T: CoordNum, Z: ZCoord, M: Measure> From<CoordTZM<T, Z, M>> for (T, T) {
fn from(coord: CoordTZM<T, Z, M>) -> Self {
(coord.x, coord.y)
}
}

impl<T: CoordNum> From<Coordinate<T>> for [T; 2] {
fn from(coord: Coordinate<T>) -> Self {
impl<T: CoordNum, Z: ZCoord, M: Measure> From<CoordTZM<T, Z, M>> for [T; 2] {
fn from(coord: CoordTZM<T, Z, M>) -> Self {
[coord.x, coord.y]
}
}

impl<T: CoordNum> Coordinate<T> {
impl<T: CoordNum, Z: ZCoord, M: Measure> CoordTZM<T, Z, M> {
/// Returns a tuple that contains the x/horizontal & y/vertical component of the coordinate.
///
/// # Examples
Expand Down
70 changes: 34 additions & 36 deletions geo-types/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
CoordNum, Error, GeometryCollection, GeometryCollectionTZM, Line, LineString, LineStringTZM,
LineTZM, Measure, MultiLineString, MultiLineStringTZM, MultiPoint, MultiPointTZM, MultiPolygon,
MultiPolygonTZM, NoValue, Point, PointTZM, Polygon, PolygonTZM, Rect, RectTZM, Triangle,
TriangleTZM, ZCoord,
CoordNum, Error, GeometryCollectionTZM, LineStringTZM, LineTZM, Measure, MultiLineStringTZM,
MultiPointTZM, MultiPolygonTZM, NoValue, PointTZM, PolygonTZM, RectTZM, TriangleTZM, ZCoord,
};

#[cfg(any(feature = "approx", test))]
Expand Down Expand Up @@ -84,32 +82,32 @@ impl<T: CoordNum, Z: ZCoord, M: Measure> From<MultiPolygonTZM<T, Z, M>> for Geom
}
}

impl<T: CoordNum> From<Rect<T>> for Geometry<T> {
fn from(x: Rect<T>) -> Self {
impl<T: CoordNum, Z: ZCoord, M: Measure> From<RectTZM<T, Z, M>> for GeometryTZM<T, Z, M> {
fn from(x: RectTZM<T, Z, M>) -> Self {
Self::Rect(x)
}
}

impl<T: CoordNum> From<Triangle<T>> for Geometry<T> {
fn from(x: Triangle<T>) -> Self {
impl<T: CoordNum, Z: ZCoord, M: Measure> From<TriangleTZM<T, Z, M>> for GeometryTZM<T, Z, M> {
fn from(x: TriangleTZM<T, Z, M>) -> Self {
Self::Triangle(x)
}
}

macro_rules! try_from_geometry_impl {
($($type: ident),+) => {
($(($type: ident, $typeTZM: ident)),+ $(,)? ) => {
$(
/// Convert a Geometry enum into its inner type.
///
/// Fails if the enum case does not match the type you are trying to convert it to.
impl <T: CoordNum> TryFrom<Geometry<T>> for $type<T> {
impl <T: CoordNum, Z: ZCoord, M: Measure> TryFrom<GeometryTZM<T, Z, M>> for $typeTZM<T, Z, M> {
type Error = Error;

fn try_from(geom: Geometry<T>) -> Result<Self, Self::Error> {
fn try_from(geom: GeometryTZM<T, Z, M>) -> Result<Self, Self::Error> {
match geom {
Geometry::$type(g) => Ok(g),
GeometryTZM::$type(g) => Ok(g),
other => Err(Error::MismatchedGeometry {
expected: type_name::<$type<T>>(),
expected: type_name::<$typeTZM<T, Z, M>>(),
found: inner_type_name(other)
})
}
Expand All @@ -119,33 +117,33 @@ macro_rules! try_from_geometry_impl {
}
}

// `concat_idents` is not available, so hacking around it
try_from_geometry_impl!(
Point,
Line,
LineString,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
Rect,
Triangle
(Point, PointTZM),
(Line, LineTZM),
(LineString, LineStringTZM),
(Polygon, PolygonTZM),
(MultiPoint, MultiPointTZM),
(MultiLineString, MultiLineStringTZM),
(MultiPolygon, MultiPolygonTZM),
(Rect, RectTZM),
(Triangle, TriangleTZM),
);

fn inner_type_name<T>(geometry: Geometry<T>) -> &'static str
where
T: CoordNum,
{
fn inner_type_name<T: CoordNum, Z: ZCoord, M: Measure>(
geometry: GeometryTZM<T, Z, M>,
) -> &'static str {
match geometry {
Geometry::Point(_) => type_name::<Point<T>>(),
Geometry::Line(_) => type_name::<Line<T>>(),
Geometry::LineString(_) => type_name::<LineString<T>>(),
Geometry::Polygon(_) => type_name::<Polygon<T>>(),
Geometry::MultiPoint(_) => type_name::<MultiPoint<T>>(),
Geometry::MultiLineString(_) => type_name::<MultiLineString<T>>(),
Geometry::MultiPolygon(_) => type_name::<MultiPolygon<T>>(),
Geometry::GeometryCollection(_) => type_name::<GeometryCollection<T>>(),
Geometry::Rect(_) => type_name::<Rect<T>>(),
Geometry::Triangle(_) => type_name::<Triangle<T>>(),
GeometryTZM::Point(_) => type_name::<PointTZM<T, Z, M>>(),
GeometryTZM::Line(_) => type_name::<LineTZM<T, Z, M>>(),
GeometryTZM::LineString(_) => type_name::<LineStringTZM<T, Z, M>>(),
GeometryTZM::Polygon(_) => type_name::<PolygonTZM<T, Z, M>>(),
GeometryTZM::MultiPoint(_) => type_name::<MultiPointTZM<T, Z, M>>(),
GeometryTZM::MultiLineString(_) => type_name::<MultiLineStringTZM<T, Z, M>>(),
GeometryTZM::MultiPolygon(_) => type_name::<MultiPolygonTZM<T, Z, M>>(),
GeometryTZM::GeometryCollection(_) => type_name::<GeometryCollectionTZM<T, Z, M>>(),
GeometryTZM::Rect(_) => type_name::<RectTZM<T, Z, M>>(),
GeometryTZM::Triangle(_) => type_name::<TriangleTZM<T, Z, M>>(),
}
}

Expand Down
22 changes: 10 additions & 12 deletions geo-types/src/line_string.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#[cfg(any(feature = "approx", test))]
use approx::{AbsDiffEq, RelativeEq};

use crate::{
CoordNum, CoordTZM, Coordinate, LineTZM, Measure, NoValue, Point, PointTZM, TriangleTZM, ZCoord,
};
use crate::{CoordNum, CoordTZM, LineTZM, Measure, NoValue, Point, PointTZM, TriangleTZM, ZCoord};
use std::iter::FromIterator;
use std::ops::{Index, IndexMut};

Expand Down Expand Up @@ -378,25 +376,25 @@ impl<'a, T: CoordNum, Z: ZCoord, M: Measure> IntoIterator for &'a LineStringTZM<
}

/// Mutably iterate over all the [`Coordinate`]s in this [`LineString`]
impl<'a, T: CoordNum> IntoIterator for &'a mut LineString<T> {
type Item = &'a mut Coordinate<T>;
type IntoIter = ::std::slice::IterMut<'a, Coordinate<T>>;
impl<'a, T: CoordNum, Z: ZCoord, M: Measure> IntoIterator for &'a mut LineStringTZM<T, Z, M> {
type Item = &'a mut CoordTZM<T, Z, M>;
type IntoIter = ::std::slice::IterMut<'a, CoordTZM<T, Z, M>>;

fn into_iter(self) -> ::std::slice::IterMut<'a, Coordinate<T>> {
fn into_iter(self) -> ::std::slice::IterMut<'a, CoordTZM<T, Z, M>> {
self.0.iter_mut()
}
}

impl<T: CoordNum> Index<usize> for LineString<T> {
type Output = Coordinate<T>;
impl<T: CoordNum, Z: ZCoord, M: Measure> Index<usize> for LineStringTZM<T, Z, M> {
type Output = CoordTZM<T, Z, M>;

fn index(&self, index: usize) -> &Coordinate<T> {
fn index(&self, index: usize) -> &CoordTZM<T, Z, M> {
self.0.index(index)
}
}

impl<T: CoordNum> IndexMut<usize> for LineString<T> {
fn index_mut(&mut self, index: usize) -> &mut Coordinate<T> {
impl<T: CoordNum, Z: ZCoord, M: Measure> IndexMut<usize> for LineStringTZM<T, Z, M> {
fn index_mut(&mut self, index: usize) -> &mut CoordTZM<T, Z, M> {
self.0.index_mut(index)
}
}
Expand Down

0 comments on commit aef15ab

Please sign in to comment.