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

Remove clone in geometry trait implementation #1121

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 38 additions & 0 deletions geo-traits/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,41 @@ impl<'a, T: CoordNum + 'a> GeometryTrait for Geometry<T> {
}
}
}

impl<'a, T: CoordNum + 'a> GeometryTrait for &'a 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(
&self,
) -> GeometryType<
'_,
Point<T>,
LineString<T>,
Polygon<T>,
MultiPoint<T>,
MultiLineString<T>,
MultiPolygon<T>,
GeometryCollection<T>,
Rect<T>,
> {
match self {
Geometry::Point(p) => GeometryType::Point(p),
Geometry::LineString(p) => GeometryType::LineString(p),
Geometry::Polygon(p) => GeometryType::Polygon(p),
Geometry::MultiPoint(p) => GeometryType::MultiPoint(p),
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!(),
}
}
}
17 changes: 8 additions & 9 deletions geo-traits/src/geometry_collection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::GeometryTrait;
use geo_types::{CoordNum, Geometry, GeometryCollection};
use std::iter::Cloned;
use std::slice::Iter;

pub trait GeometryCollectionTrait {
Expand All @@ -25,41 +24,41 @@ pub trait GeometryCollectionTrait {

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

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

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

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

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

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

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

fn geometry(&self, i: usize) -> Option<Self::ItemType<'_>> {
self.0.get(i).cloned()
self.0.get(i)
}
}
18 changes: 8 additions & 10 deletions geo-traits/src/line_string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use geo_types::{Coord, CoordNum, LineString};

use super::CoordTrait;
use std::iter::Cloned;
use std::slice::Iter;

pub trait LineStringTrait {
Expand All @@ -26,37 +25,36 @@ pub trait LineStringTrait {

impl<T: CoordNum> LineStringTrait for LineString<T> {
type T = T;
type ItemType<'a> = Coord<Self::T> where Self: 'a;
type Iter<'a> = Cloned<Iter<'a, Self::ItemType<'a>>> where T: 'a;
type ItemType<'a> = &'a Coord<Self::T> where Self: 'a;
type Iter<'a> = Iter<'a, Coord<Self::T>> where T: 'a;

fn coords(&self) -> Self::Iter<'_> {
// TODO: remove cloned
self.0.iter().cloned()
self.0.iter()
}

fn num_coords(&self) -> usize {
self.0.len()
}

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

impl<'a, T: CoordNum> LineStringTrait for &'a LineString<T> {
type T = T;
type ItemType<'b> = Coord<Self::T> where Self: 'b;
type Iter<'b> = Cloned<Iter<'a, Self::ItemType<'a>>> where Self: 'b;
type ItemType<'b> = &'a Coord<Self::T> where Self: 'b;
type Iter<'b> = Iter<'a, Coord<Self::T>> where Self: 'b;

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

fn num_coords(&self) -> usize {
self.0.len()
}

fn coord(&self, i: usize) -> Option<Self::ItemType<'_>> {
self.0.get(i).cloned()
self.0.get(i)
}
}
17 changes: 8 additions & 9 deletions geo-traits/src/multi_line_string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::line_string::LineStringTrait;
use geo_types::{CoordNum, LineString, MultiLineString};
use std::iter::Cloned;
use std::slice::Iter;

pub trait MultiLineStringTrait {
Expand All @@ -25,36 +24,36 @@ pub trait MultiLineStringTrait {

impl<T: CoordNum> MultiLineStringTrait for MultiLineString<T> {
type T = T;
type ItemType<'a> = LineString<Self::T> where Self: 'a;
type Iter<'a> = Cloned<Iter<'a, Self::ItemType<'a>>> where T: 'a;
type ItemType<'a> = &'a LineString<Self::T> where Self: 'a;
type Iter<'a> = Iter<'a, LineString<Self::T>> where T: 'a;

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

fn num_lines(&self) -> usize {
self.0.len()
}

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

impl<'a, T: CoordNum> MultiLineStringTrait for &'a MultiLineString<T> {
type T = T;
type ItemType<'b> = LineString<Self::T> where Self: 'b;
type Iter<'b> = Cloned<Iter<'a, Self::ItemType<'a>>> where Self: 'b;
type ItemType<'b> = &'a LineString<Self::T> where Self: 'b;
type Iter<'b> = Iter<'a, LineString<Self::T>> where Self: 'b;

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

fn num_lines(&self) -> usize {
self.0.len()
}

fn line(&self, i: usize) -> Option<Self::ItemType<'_>> {
self.0.get(i).cloned()
self.0.get(i)
}
}
17 changes: 8 additions & 9 deletions geo-traits/src/multi_point.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::point::PointTrait;
use geo_types::{CoordNum, MultiPoint, Point};
use std::iter::Cloned;
use std::slice::Iter;

pub trait MultiPointTrait {
Expand All @@ -25,36 +24,36 @@ pub trait MultiPointTrait {

impl<T: CoordNum> MultiPointTrait for MultiPoint<T> {
type T = T;
type ItemType<'a> = Point<Self::T> where Self: 'a;
type Iter<'a> = Cloned<Iter<'a, Self::ItemType<'a>>> where T: 'a;
type ItemType<'a> = &'a Point<Self::T> where Self: 'a;
type Iter<'a> = Iter<'a, Point<Self::T>> where T: 'a;

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

fn num_points(&self) -> usize {
self.0.len()
}

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

impl<'a, T: CoordNum> MultiPointTrait for &'a MultiPoint<T> {
type T = T;
type ItemType<'b> = Point<Self::T> where Self: 'b;
type Iter<'b> = Cloned<Iter<'a, Self::ItemType<'a>>> where Self: 'b;
type ItemType<'b> = &'a Point<Self::T> where Self: 'b;
type Iter<'b> = Iter<'a, Point<Self::T>> where Self: 'b;

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

fn num_points(&self) -> usize {
self.0.len()
}

fn point(&self, i: usize) -> Option<Self::ItemType<'_>> {
self.0.get(i).cloned()
self.0.get(i)
}
}
17 changes: 8 additions & 9 deletions geo-traits/src/multi_polygon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::polygon::PolygonTrait;
use geo_types::{CoordNum, MultiPolygon, Polygon};
use std::iter::Cloned;
use std::slice::Iter;

pub trait MultiPolygonTrait {
Expand All @@ -25,36 +24,36 @@ pub trait MultiPolygonTrait {

impl<T: CoordNum> MultiPolygonTrait for MultiPolygon<T> {
type T = T;
type ItemType<'a> = Polygon<Self::T> where Self: 'a;
type Iter<'a> = Cloned<Iter<'a, Self::ItemType<'a>>> where T: 'a;
type ItemType<'a> = &'a Polygon<Self::T> where Self: 'a;
type Iter<'a> = Iter<'a, Polygon<Self::T>> where T: 'a;

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

fn num_polygons(&self) -> usize {
self.0.len()
}

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

impl<'a, T: CoordNum> MultiPolygonTrait for &'a MultiPolygon<T> {
type T = T;
type ItemType<'b> = Polygon<Self::T> where Self: 'b;
type Iter<'b> = Cloned<Iter<'a, Self::ItemType<'a>>> where Self: 'b;
type ItemType<'b> = &'a Polygon<Self::T> where Self: 'b;
type Iter<'b> = Iter<'a, Polygon<Self::T>> where Self: 'b;

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

fn num_polygons(&self) -> usize {
self.0.len()
}

fn polygon(&self, i: usize) -> Option<Self::ItemType<'_>> {
self.0.get(i).cloned()
self.0.get(i)
}
}
21 changes: 10 additions & 11 deletions geo-traits/src/polygon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::line_string::LineStringTrait;
use geo_types::{CoordNum, LineString, Polygon};
use std::iter::Cloned;
use std::slice::Iter;

pub trait PolygonTrait {
Expand Down Expand Up @@ -28,47 +27,47 @@ pub trait PolygonTrait {

impl<T: CoordNum> PolygonTrait for Polygon<T> {
type T = T;
type ItemType<'a> = LineString<Self::T> where Self: 'a;
type Iter<'a> = Cloned<Iter<'a, Self::ItemType<'a>>> where T: 'a;
type ItemType<'a> = &'a LineString<Self::T> where Self: 'a;
type Iter<'a> = Iter<'a, LineString<Self::T>> where T: 'a;

fn exterior(&self) -> Option<Self::ItemType<'_>> {
// geo-types doesn't really have a way to describe an empty polygon
Some(Polygon::exterior(self).clone())
Some(Polygon::exterior(self))
}

fn interiors(&self) -> Self::Iter<'_> {
Polygon::interiors(self).iter().cloned()
Polygon::interiors(self).iter()
}

fn num_interiors(&self) -> usize {
Polygon::interiors(self).len()
}

fn interior(&self, i: usize) -> Option<Self::ItemType<'_>> {
Polygon::interiors(self).get(i).cloned()
Polygon::interiors(self).get(i)
}
}

impl<'a, T: CoordNum> PolygonTrait for &'a Polygon<T> {
type T = T;
type ItemType<'b> = LineString<Self::T> where
type ItemType<'b> = &'a LineString<Self::T> where
Self: 'b;
type Iter<'b> = Cloned<Iter<'a, Self::ItemType<'a>>> where
type Iter<'b> = Iter<'a, LineString<Self::T>> where
Self: 'b;

fn exterior(&self) -> Option<Self::ItemType<'_>> {
Some(Polygon::exterior(self).clone())
Some(Polygon::exterior(self))
}

fn interiors(&self) -> Self::Iter<'_> {
Polygon::interiors(self).iter().cloned()
Polygon::interiors(self).iter()
}

fn num_interiors(&self) -> usize {
Polygon::interiors(self).len()
}

fn interior(&self, i: usize) -> Option<Self::ItemType<'_>> {
Polygon::interiors(self).get(i).cloned()
Polygon::interiors(self).get(i)
}
}
Loading