From 76cece6d2caa1764e67d6354f58ae3efb5fc391e Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Mon, 23 Dec 2024 17:59:44 -0500 Subject: [PATCH] Add UnimplementedGeometryCollection --- geo-traits/src/geometry.rs | 78 +++++++++++++++++++++++++++ geo-traits/src/geometry_collection.rs | 30 ++++++++++- geo-traits/src/lib.rs | 4 +- 3 files changed, 109 insertions(+), 3 deletions(-) diff --git a/geo-traits/src/geometry.rs b/geo-traits/src/geometry.rs index b1ed8cd59..61f7f4c83 100644 --- a/geo-traits/src/geometry.rs +++ b/geo-traits/src/geometry.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + #[cfg(feature = "geo-types")] use geo_types::{ CoordNum, Geometry, GeometryCollection, Line, LineString, MultiLineString, MultiPoint, @@ -7,6 +9,9 @@ use geo_types::{ use crate::{ Dimensions, GeometryCollectionTrait, LineStringTrait, LineTrait, MultiLineStringTrait, MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait, RectTrait, TriangleTrait, + UnimplementedGeometryCollection, UnimplementedLine, UnimplementedLineString, + UnimplementedMultiLineString, UnimplementedMultiPoint, UnimplementedMultiPolygon, + UnimplementedPoint, UnimplementedPolygon, UnimplementedRect, UnimplementedTriangle, }; /// A trait for accessing data from a generic Geometry. @@ -431,3 +436,76 @@ impl_specialization!(GeometryCollection); impl_specialization!(Rect); impl_specialization!(Triangle); impl_specialization!(Line); + +/// An empty struct that implements [GeometryTrait]. +/// +/// This is used internally for [`UnimplementedGeometryCollection`], so that +/// `UnimplementedGeometryCollection` can be used as the `GeometryCollectionType` of the +/// `GeometryTrait` by implementations that don't have a GeometryCollection concept +pub struct UnimplementedGeometry(PhantomData); + +impl GeometryTrait for UnimplementedGeometry { + type T = T; + type PointType<'b> + = UnimplementedPoint + where + Self: 'b; + type LineStringType<'b> + = UnimplementedLineString + where + Self: 'b; + type PolygonType<'b> + = UnimplementedPolygon + where + Self: 'b; + type MultiPointType<'b> + = UnimplementedMultiPoint + where + Self: 'b; + type MultiLineStringType<'b> + = UnimplementedMultiLineString + where + Self: 'b; + type MultiPolygonType<'b> + = UnimplementedMultiPolygon + where + Self: 'b; + type GeometryCollectionType<'b> + = UnimplementedGeometryCollection + where + Self: 'b; + type RectType<'b> + = UnimplementedRect + where + Self: 'b; + type TriangleType<'b> + = UnimplementedTriangle + where + Self: 'b; + type LineType<'b> + = UnimplementedLine + where + Self: 'b; + + fn dim(&self) -> Dimensions { + unimplemented!() + } + + fn as_type( + &self, + ) -> GeometryType< + '_, + Self::PointType<'_>, + Self::LineStringType<'_>, + Self::PolygonType<'_>, + Self::MultiPointType<'_>, + Self::MultiLineStringType<'_>, + Self::MultiPolygonType<'_>, + Self::GeometryCollectionType<'_>, + Self::RectType<'_>, + Self::TriangleType<'_>, + Self::LineType<'_>, + > { + unimplemented!() + } +} diff --git a/geo-traits/src/geometry_collection.rs b/geo-traits/src/geometry_collection.rs index 8a2a74ab2..9bbc83ba5 100644 --- a/geo-traits/src/geometry_collection.rs +++ b/geo-traits/src/geometry_collection.rs @@ -1,5 +1,7 @@ +use std::marker::PhantomData; + use crate::iterator::GeometryCollectionIterator; -use crate::{Dimensions, GeometryTrait}; +use crate::{Dimensions, GeometryTrait, UnimplementedGeometry}; #[cfg(feature = "geo-types")] use geo_types::{CoordNum, Geometry, GeometryCollection}; @@ -87,3 +89,29 @@ impl<'a, T: CoordNum> GeometryCollectionTrait for &'a GeometryCollection { self.0.get_unchecked(i) } } + +/// An empty struct that implements [GeometryCollectionTrait]. +/// +/// This can be used as the `GeometryCollectionType` of the `GeometryTrait` by implementations that +/// don't have a GeometryCollection concept +pub struct UnimplementedGeometryCollection(PhantomData); + +impl GeometryCollectionTrait for UnimplementedGeometryCollection { + type T = T; + type GeometryType<'a> + = UnimplementedGeometry + where + Self: 'a; + + fn dim(&self) -> Dimensions { + unimplemented!() + } + + fn num_geometries(&self) -> usize { + unimplemented!() + } + + unsafe fn geometry_unchecked(&self, _i: usize) -> Self::GeometryType<'_> { + unimplemented!() + } +} diff --git a/geo-traits/src/lib.rs b/geo-traits/src/lib.rs index 9b63805f3..b307b7325 100644 --- a/geo-traits/src/lib.rs +++ b/geo-traits/src/lib.rs @@ -19,8 +19,8 @@ pub use coord::{CoordTrait, UnimplementedCoord}; pub use dimension::Dimensions; -pub use geometry::{GeometryTrait, GeometryType}; -pub use geometry_collection::GeometryCollectionTrait; +pub use geometry::{GeometryTrait, GeometryType, UnimplementedGeometry}; +pub use geometry_collection::{GeometryCollectionTrait, UnimplementedGeometryCollection}; pub use line::{LineTrait, UnimplementedLine}; pub use line_string::{LineStringTrait, UnimplementedLineString}; pub use multi_line_string::{MultiLineStringTrait, UnimplementedMultiLineString};