Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Area based on traits #1115

Draft
wants to merge 4 commits into
base: traits
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove unneeded trait lifetime in geo-traits
kylebarron committed Nov 17, 2023

Verified

This commit was signed with the committer’s verified signature.
sergio-costas Sergio Costas
commit fd19c5db7acd3ef93137b8f4decb75df5e0b8cc4
40 changes: 26 additions & 14 deletions geo-traits/src/coord.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use geo_types::{Coord, CoordNum, Point};

pub trait CoordTrait: Send + Sync {
type T: CoordNum + Send + Sync;
pub trait CoordTrait {
type T: CoordNum;

/// x component of this coord
fn x(&self) -> Self::T;
@@ -15,50 +15,62 @@ pub trait CoordTrait: Send + Sync {
}
}

impl<T: CoordNum + Send + Sync> CoordTrait for Point<T> {
impl<T: CoordNum> CoordTrait for Point<T> {
type T = T;

fn x(&self) -> T {
fn x(&self) -> Self::T {
self.0.x
}

fn y(&self) -> T {
fn y(&self) -> Self::T {
self.0.y
}
}

impl<T: CoordNum + Send + Sync> CoordTrait for &Point<T> {
impl<T: CoordNum> CoordTrait for &Point<T> {
type T = T;

fn x(&self) -> T {
fn x(&self) -> Self::T {
self.0.x
}

fn y(&self) -> T {
fn y(&self) -> Self::T {
self.0.y
}
}

impl<T: CoordNum + Send + Sync> CoordTrait for Coord<T> {
impl<T: CoordNum> CoordTrait for Coord<T> {
type T = T;

fn x(&self) -> T {
fn x(&self) -> Self::T {
self.x
}

fn y(&self) -> T {
fn y(&self) -> Self::T {
self.y
}
}

impl<T: CoordNum + Send + Sync> CoordTrait for &Coord<T> {
impl<T: CoordNum> CoordTrait for &Coord<T> {
type T = T;

fn x(&self) -> T {
fn x(&self) -> Self::T {
self.x
}

fn y(&self) -> T {
fn y(&self) -> Self::T {
self.y
}
}

impl<T: CoordNum> CoordTrait for (T, T) {
type T = T;

fn x(&self) -> Self::T {
self.0
}

fn y(&self) -> Self::T {
self.1
}
}
99 changes: 62 additions & 37 deletions geo-traits/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,67 @@
use geo_types::{
CoordNum, Geometry, GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon,
Point, Polygon,
Point, Polygon, Rect,
};

use super::{
GeometryCollectionTrait, LineStringTrait, MultiLineStringTrait, MultiPointTrait,
MultiPolygonTrait, PointTrait, PolygonTrait,
MultiPolygonTrait, PointTrait, PolygonTrait, RectTrait,
};

#[allow(clippy::type_complexity)]
pub trait GeometryTrait<'a>: Send + Sync {
type Point: 'a + PointTrait;
type LineString: 'a + LineStringTrait<'a>;
type Polygon: 'a + PolygonTrait<'a>;
type MultiPoint: 'a + MultiPointTrait<'a>;
type MultiLineString: 'a + MultiLineStringTrait<'a>;
type MultiPolygon: 'a + MultiPolygonTrait<'a>;
type GeometryCollection: 'a + GeometryCollectionTrait<'a>;
pub trait GeometryTrait {
type T: CoordNum;
type Point<'a>: 'a + PointTrait<T = Self::T>
where
Self: 'a;
type LineString<'a>: 'a + LineStringTrait<T = Self::T>
where
Self: 'a;
type Polygon<'a>: 'a + PolygonTrait<T = Self::T>
where
Self: 'a;
type MultiPoint<'a>: 'a + MultiPointTrait<T = Self::T>
where
Self: 'a;
type MultiLineString<'a>: 'a + MultiLineStringTrait<T = Self::T>
where
Self: 'a;
type MultiPolygon<'a>: 'a + MultiPolygonTrait<T = Self::T>
where
Self: 'a;
type GeometryCollection<'a>: 'a + GeometryCollectionTrait<T = Self::T>
where
Self: 'a;
type Rect<'a>: 'a + RectTrait<T = Self::T>
where
Self: 'a;

fn as_type(
&'a self,
&self,
) -> GeometryType<
'a,
Self::Point,
Self::LineString,
Self::Polygon,
Self::MultiPoint,
Self::MultiLineString,
Self::MultiPolygon,
Self::GeometryCollection,
'_,
Self::Point<'_>,
Self::LineString<'_>,
Self::Polygon<'_>,
Self::MultiPoint<'_>,
Self::MultiLineString<'_>,
Self::MultiPolygon<'_>,
Self::GeometryCollection<'_>,
Self::Rect<'_>,
>;
}

#[derive(Debug)]
pub enum GeometryType<'a, P, L, Y, MP, ML, MY, GC>
pub enum GeometryType<'a, P, L, Y, MP, ML, MY, GC, R>
where
P: 'a + PointTrait,
L: 'a + LineStringTrait<'a>,
Y: 'a + PolygonTrait<'a>,
MP: 'a + MultiPointTrait<'a>,
ML: 'a + MultiLineStringTrait<'a>,
MY: 'a + MultiPolygonTrait<'a>,
GC: 'a + GeometryCollectionTrait<'a>,
P: PointTrait,
L: LineStringTrait,
Y: PolygonTrait,
MP: MultiPointTrait,
ML: MultiLineStringTrait,
MY: MultiPolygonTrait,
GC: GeometryCollectionTrait,
R: RectTrait,
{
Point(&'a P),
LineString(&'a L),
@@ -50,28 +70,32 @@ where
MultiLineString(&'a ML),
MultiPolygon(&'a MY),
GeometryCollection(&'a GC),
Rect(&'a R),
}

impl<'a, T: CoordNum + Send + Sync + 'a> GeometryTrait<'a> for Geometry<T> {
type Point = Point<T>;
type LineString = LineString<T>;
type Polygon = Polygon<T>;
type MultiPoint = MultiPoint<T>;
type MultiLineString = MultiLineString<T>;
type MultiPolygon = MultiPolygon<T>;
type GeometryCollection = GeometryCollection<T>;
impl<'a, T: CoordNum + 'a> GeometryTrait for Geometry<T> {
type T = T;
type Point<'b> = Point<Self::T> where Self: 'b;
type LineString<'b> = LineString<Self::T> where Self: 'b;
type Polygon<'b> = Polygon<Self::T> where Self: 'b;
type MultiPoint<'b> = MultiPoint<Self::T> where Self: 'b;
type MultiLineString<'b> = MultiLineString<Self::T> where Self: 'b;
type MultiPolygon<'b> = MultiPolygon<Self::T> where Self: 'b;
type GeometryCollection<'b> = GeometryCollection<Self::T> where Self: 'b;
type Rect<'b> = Rect<Self::T> where Self: 'b;

fn as_type(
&'a self,
&self,
) -> GeometryType<
'a,
'_,
Point<T>,
LineString<T>,
Polygon<T>,
MultiPoint<T>,
MultiLineString<T>,
MultiPolygon<T>,
GeometryCollection<T>,
Rect<T>,
> {
match self {
Geometry::Point(p) => GeometryType::Point(p),
@@ -81,6 +105,7 @@ impl<'a, T: CoordNum + Send + Sync + 'a> GeometryTrait<'a> for Geometry<T> {
Geometry::MultiLineString(p) => GeometryType::MultiLineString(p),
Geometry::MultiPolygon(p) => GeometryType::MultiPolygon(p),
Geometry::GeometryCollection(p) => GeometryType::GeometryCollection(p),
Geometry::Rect(p) => GeometryType::Rect(p),
_ => todo!(),
}
}
48 changes: 30 additions & 18 deletions geo-traits/src/geometry_collection.rs
Original file line number Diff line number Diff line change
@@ -3,51 +3,63 @@ use geo_types::{CoordNum, Geometry, GeometryCollection};
use std::iter::Cloned;
use std::slice::Iter;

pub trait GeometryCollectionTrait<'a>: Send + Sync {
type ItemType: 'a + GeometryTrait<'a>;
type Iter: ExactSizeIterator<Item = Self::ItemType>;
pub trait GeometryCollectionTrait {
type T: CoordNum;
type ItemType<'a>: 'a + GeometryTrait<T = Self::T>
where
Self: 'a;
type Iter<'a>: ExactSizeIterator<Item = Self::ItemType<'a>>
where
Self: 'a;

/// An iterator over the geometries in this GeometryCollection
fn geometries(&'a self) -> Self::Iter;
fn geometries(&self) -> Self::Iter<'_>;

/// The number of geometries in this GeometryCollection
fn num_geometries(&'a self) -> usize;
fn num_geometries(&self) -> usize;

/// Access to a specified geometry in this GeometryCollection
/// Will return None if the provided index is out of bounds
fn geometry(&'a self, i: usize) -> Option<Self::ItemType>;
fn geometry(&self, i: usize) -> Option<Self::ItemType<'_>>;
}

impl<'a, T: CoordNum + Send + Sync + 'a> GeometryCollectionTrait<'a> for GeometryCollection<T> {
type ItemType = Geometry<T>;
type Iter = Cloned<Iter<'a, Self::ItemType>>;
impl<T: CoordNum> GeometryCollectionTrait for GeometryCollection<T> {
type T = T;
type ItemType<'a> = Geometry<Self::T>
where
Self: 'a;
type Iter<'a> = Cloned<Iter<'a, Self::ItemType<'a>>>
where T: 'a;

fn geometries(&'a self) -> Self::Iter {
fn geometries(&self) -> Self::Iter<'_> {
self.0.iter().cloned()
}

fn num_geometries(&'a self) -> usize {
fn num_geometries(&self) -> usize {
self.0.len()
}

fn geometry(&'a self, i: usize) -> Option<Self::ItemType> {
fn geometry(&self, i: usize) -> Option<Self::ItemType<'_>> {
self.0.get(i).cloned()
}
}

impl<'a, T: CoordNum + Send + Sync + 'a> GeometryCollectionTrait<'a> for &GeometryCollection<T> {
type ItemType = Geometry<T>;
type Iter = Cloned<Iter<'a, Self::ItemType>>;
impl<'a, T: CoordNum> GeometryCollectionTrait for &'a GeometryCollection<T> {
type T = T;
type ItemType<'b> = Geometry<Self::T> where
Self: 'b;
type Iter<'b> = Cloned<Iter<'a, Self::ItemType<'a>>> where
Self: 'b;

fn geometries(&'a self) -> Self::Iter {
fn geometries(&self) -> Self::Iter<'_> {
self.0.iter().cloned()
}

fn num_geometries(&'a self) -> usize {
fn num_geometries(&self) -> usize {
self.0.len()
}

fn geometry(&'a self, i: usize) -> Option<Self::ItemType> {
fn geometry(&self, i: usize) -> Option<Self::ItemType<'_>> {
self.0.get(i).cloned()
}
}
2 changes: 2 additions & 0 deletions geo-traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ pub use multi_point::MultiPointTrait;
pub use multi_polygon::MultiPolygonTrait;
pub use point::PointTrait;
pub use polygon::PolygonTrait;
pub use rect::RectTrait;

mod coord;
mod geometry;
@@ -17,3 +18,4 @@ mod multi_point;
mod multi_polygon;
mod point;
mod polygon;
mod rect;
Loading