Skip to content

Commit

Permalink
Merge pull request #1241 from kylebarron/kyle/traits-optional-types-dep
Browse files Browse the repository at this point in the history
geo-traits: Make `geo-types` dependency optional
  • Loading branch information
michaelkirk authored Oct 31, 2024
2 parents ba1cf70 + 4afa811 commit ceca778
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 44 deletions.
5 changes: 4 additions & 1 deletion geo-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ description = "Geospatial traits"
rust-version = "1.65"
edition = "2021"

[features]
default = ["geo-types"]

[dependencies]
geo-types = "0.7"
geo-types = { version = "0.7", optional = true }

[dev-dependencies]
11 changes: 7 additions & 4 deletions geo-traits/src/coord.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::marker::PhantomData;

#[cfg(feature = "geo-types")]
use geo_types::{Coord, CoordNum};

use crate::Dimensions;
Expand All @@ -9,7 +10,7 @@ use crate::Dimensions;
/// Refer to [geo_types::Coord] for information about semantics and validity.
pub trait CoordTrait {
/// The coordinate type of this geometry
type T: CoordNum;
type T;

/// Dimensions of the coordinate tuple
fn dim(&self) -> Dimensions;
Expand Down Expand Up @@ -65,6 +66,7 @@ pub trait CoordTrait {
}
}

#[cfg(feature = "geo-types")]
impl<T: CoordNum> CoordTrait for Coord<T> {
type T = T;

Expand All @@ -89,6 +91,7 @@ impl<T: CoordNum> CoordTrait for Coord<T> {
}
}

#[cfg(feature = "geo-types")]
impl<T: CoordNum> CoordTrait for &Coord<T> {
type T = T;

Expand All @@ -113,7 +116,7 @@ impl<T: CoordNum> CoordTrait for &Coord<T> {
}
}

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

fn nth_or_panic(&self, n: usize) -> Self::T {
Expand Down Expand Up @@ -141,9 +144,9 @@ impl<T: CoordNum> CoordTrait for (T, T) {
///
/// This can be used as the `CoordType` of the `GeometryTrait` by implementations that don't have a
/// Coord concept
pub struct UnimplementedCoord<T: CoordNum>(PhantomData<T>);
pub struct UnimplementedCoord<T>(PhantomData<T>);

impl<T: CoordNum> CoordTrait for UnimplementedCoord<T> {
impl<T> CoordTrait for UnimplementedCoord<T> {
type T = T;

fn dim(&self) -> Dimensions {
Expand Down
7 changes: 6 additions & 1 deletion geo-traits/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "geo-types")]
use geo_types::{
CoordNum, Geometry, GeometryCollection, Line, LineString, MultiLineString, MultiPoint,
MultiPolygon, Point, Polygon, Rect, Triangle,
Expand All @@ -12,7 +13,7 @@ use crate::{
#[allow(clippy::type_complexity)]
pub trait GeometryTrait {
/// The coordinate type of this geometry
type T: CoordNum;
type T;

/// The type of each underlying Point, which implements [PointTrait]
type PointType<'a>: 'a + PointTrait<T = Self::T>
Expand Down Expand Up @@ -124,6 +125,7 @@ where
Line(&'a L),
}

#[cfg(feature = "geo-types")]
impl<'a, T: CoordNum + 'a> GeometryTrait for Geometry<T> {
type T = T;
type PointType<'b> = Point<Self::T> where Self: 'b;
Expand Down Expand Up @@ -171,6 +173,7 @@ impl<'a, T: CoordNum + 'a> GeometryTrait for Geometry<T> {
}
}

#[cfg(feature = "geo-types")]
impl<'a, T: CoordNum + 'a> GeometryTrait for &'a Geometry<T> {
type T = T;
type PointType<'b> = Point<Self::T> where Self: 'b;
Expand Down Expand Up @@ -222,6 +225,7 @@ impl<'a, T: CoordNum + 'a> GeometryTrait for &'a Geometry<T> {

macro_rules! impl_specialization {
($geometry_type:ident) => {
#[cfg(feature = "geo-types")]
impl<T: CoordNum> GeometryTrait for $geometry_type<T> {
type T = T;
type PointType<'b> = Point<Self::T> where Self: 'b;
Expand Down Expand Up @@ -258,6 +262,7 @@ macro_rules! impl_specialization {
}
}

#[cfg(feature = "geo-types")]
impl<'a, T: CoordNum + 'a> GeometryTrait for &'a $geometry_type<T> {
type T = T;
type PointType<'b> = Point<Self::T> where Self: 'b;
Expand Down
5 changes: 4 additions & 1 deletion geo-traits/src/geometry_collection.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::iterator::GeometryCollectionIterator;
use crate::{Dimensions, GeometryTrait};
#[cfg(feature = "geo-types")]
use geo_types::{CoordNum, Geometry, GeometryCollection};

/// A trait for accessing data from a generic GeometryCollection.
///
/// A GeometryCollection is a collection of [Geometry][GeometryTrait] types.
pub trait GeometryCollectionTrait: Sized {
/// The coordinate type of this geometry
type T: CoordNum;
type T;

/// The type of each underlying geometry, which implements [GeometryTrait]
type GeometryType<'a>: 'a + GeometryTrait<T = Self::T>
Expand Down Expand Up @@ -45,6 +46,7 @@ pub trait GeometryCollectionTrait: Sized {
unsafe fn geometry_unchecked(&self, i: usize) -> Self::GeometryType<'_>;
}

#[cfg(feature = "geo-types")]
impl<T: CoordNum> GeometryCollectionTrait for GeometryCollection<T> {
type T = T;
type GeometryType<'a> = &'a Geometry<Self::T>
Expand All @@ -64,6 +66,7 @@ impl<T: CoordNum> GeometryCollectionTrait for GeometryCollection<T> {
}
}

#[cfg(feature = "geo-types")]
impl<'a, T: CoordNum> GeometryCollectionTrait for &'a GeometryCollection<T> {
type T = T;
type GeometryType<'b> = &'a Geometry<Self::T> where
Expand Down
17 changes: 7 additions & 10 deletions geo-traits/src/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use crate::CoordTrait;

use super::{
GeometryCollectionTrait, GeometryTrait, LineStringTrait, MultiLineStringTrait, MultiPointTrait,
MultiPolygonTrait, PointTrait, PolygonTrait,
CoordTrait, GeometryCollectionTrait, GeometryTrait, LineStringTrait, MultiLineStringTrait,
MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait,
};
use geo_types::CoordNum;

macro_rules! impl_iterator {
($struct_name:ident, $self_trait:ident, $item_trait:ident, $access_method:ident, $item_type:ident) => {
/// An iterator over the parts of this geometry.
pub(crate) struct $struct_name<
'a,
T: CoordNum,
T,
$item_type: 'a + $item_trait<T = T>,
G: $self_trait<T = T, $item_type<'a> = $item_type>,
> {
Expand All @@ -22,7 +19,7 @@ macro_rules! impl_iterator {

impl<
'a,
T: CoordNum,
T,
$item_type: 'a + $item_trait<T = T>,
G: $self_trait<T = T, $item_type<'a> = $item_type>,
> $struct_name<'a, T, $item_type, G>
Expand All @@ -35,7 +32,7 @@ macro_rules! impl_iterator {

impl<
'a,
T: CoordNum,
T,
$item_type: 'a + $item_trait<T = T>,
G: $self_trait<T = T, $item_type<'a> = $item_type>,
> Iterator for $struct_name<'a, T, $item_type, G>
Expand All @@ -60,7 +57,7 @@ macro_rules! impl_iterator {

impl<
'a,
T: CoordNum,
T,
$item_type: 'a + $item_trait<T = T>,
G: $self_trait<T = T, $item_type<'a> = $item_type>,
> ExactSizeIterator for $struct_name<'a, T, $item_type, G>
Expand All @@ -69,7 +66,7 @@ macro_rules! impl_iterator {

impl<
'a,
T: CoordNum,
T,
$item_type: 'a + $item_trait<T = T>,
G: $self_trait<T = T, $item_type<'a> = $item_type>,
> DoubleEndedIterator for $struct_name<'a, T, $item_type, G>
Expand Down
9 changes: 6 additions & 3 deletions geo-traits/src/line.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::marker::PhantomData;

use crate::{CoordTrait, Dimensions, UnimplementedCoord};
#[cfg(feature = "geo-types")]
use geo_types::{Coord, CoordNum, Line};

/// A trait for accessing data from a generic Line.
Expand All @@ -10,7 +11,7 @@ use geo_types::{Coord, CoordNum, Line};
/// Refer to [geo_types::Line] for information about semantics and validity.
pub trait LineTrait: Sized {
/// The coordinate type of this geometry
type T: CoordNum;
type T;

/// The type of each underlying coordinate, which implements [CoordTrait]
type CoordType<'a>: 'a + CoordTrait<T = Self::T>
Expand All @@ -32,6 +33,7 @@ pub trait LineTrait: Sized {
}
}

#[cfg(feature = "geo-types")]
impl<T: CoordNum> LineTrait for Line<T> {
type T = T;
type CoordType<'a> = &'a Coord<Self::T> where Self: 'a;
Expand All @@ -49,6 +51,7 @@ impl<T: CoordNum> LineTrait for Line<T> {
}
}

#[cfg(feature = "geo-types")]
impl<'a, T: CoordNum> LineTrait for &'a Line<T> {
type T = T;
type CoordType<'b> = &'a Coord<Self::T> where Self: 'b;
Expand All @@ -70,9 +73,9 @@ impl<'a, T: CoordNum> LineTrait for &'a Line<T> {
///
/// This can be used as the `LineType` of the `GeometryTrait` by implementations that don't
/// have a Line concept
pub struct UnimplementedLine<T: CoordNum>(PhantomData<T>);
pub struct UnimplementedLine<T>(PhantomData<T>);

impl<T: CoordNum> LineTrait for UnimplementedLine<T> {
impl<T> LineTrait for UnimplementedLine<T> {
type T = T;
type CoordType<'a> = UnimplementedCoord<Self::T> where Self: 'a;

Expand Down
9 changes: 6 additions & 3 deletions geo-traits/src/line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::marker::PhantomData;

use crate::iterator::LineStringIterator;
use crate::{CoordTrait, Dimensions, UnimplementedCoord};
#[cfg(feature = "geo-types")]
use geo_types::{Coord, CoordNum, LineString};

/// A trait for accessing data from a generic LineString.
Expand All @@ -12,7 +13,7 @@ use geo_types::{Coord, CoordNum, LineString};
/// Refer to [geo_types::LineString] for information about semantics and validity.
pub trait LineStringTrait: Sized {
/// The coordinate type of this geometry
type T: CoordNum;
type T;

/// The type of each underlying coordinate, which implements [CoordTrait]
type CoordType<'a>: 'a + CoordTrait<T = Self::T>
Expand Down Expand Up @@ -49,6 +50,7 @@ pub trait LineStringTrait: Sized {
unsafe fn coord_unchecked(&self, i: usize) -> Self::CoordType<'_>;
}

#[cfg(feature = "geo-types")]
impl<T: CoordNum> LineStringTrait for LineString<T> {
type T = T;
type CoordType<'a> = &'a Coord<Self::T> where Self: 'a;
Expand All @@ -66,6 +68,7 @@ impl<T: CoordNum> LineStringTrait for LineString<T> {
}
}

#[cfg(feature = "geo-types")]
impl<'a, T: CoordNum> LineStringTrait for &'a LineString<T> {
type T = T;
type CoordType<'b> = &'a Coord<Self::T> where Self: 'b;
Expand All @@ -87,9 +90,9 @@ impl<'a, T: CoordNum> LineStringTrait for &'a LineString<T> {
///
/// This can be used as the `LineStringType` of the `GeometryTrait` by implementations that don't
/// have a LineString concept
pub struct UnimplementedLineString<T: CoordNum>(PhantomData<T>);
pub struct UnimplementedLineString<T>(PhantomData<T>);

impl<T: CoordNum> LineStringTrait for UnimplementedLineString<T> {
impl<T> LineStringTrait for UnimplementedLineString<T> {
type T = T;
type CoordType<'a> = UnimplementedCoord<Self::T> where Self: 'a;

Expand Down
9 changes: 6 additions & 3 deletions geo-traits/src/multi_line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::marker::PhantomData;
use crate::iterator::MultiLineStringIterator;
use crate::line_string::UnimplementedLineString;
use crate::{Dimensions, LineStringTrait};
#[cfg(feature = "geo-types")]
use geo_types::{CoordNum, LineString, MultiLineString};

/// A trait for accessing data from a generic MultiLineString.
Expand All @@ -12,7 +13,7 @@ use geo_types::{CoordNum, LineString, MultiLineString};
/// Refer to [geo_types::MultiLineString] for information about semantics and validity.
pub trait MultiLineStringTrait: Sized {
/// The coordinate type of this geometry
type T: CoordNum;
type T;

/// The type of each underlying LineString, which implements [LineStringTrait]
type LineStringType<'a>: 'a + LineStringTrait<T = Self::T>
Expand Down Expand Up @@ -50,6 +51,7 @@ pub trait MultiLineStringTrait: Sized {
unsafe fn line_string_unchecked(&self, i: usize) -> Self::LineStringType<'_>;
}

#[cfg(feature = "geo-types")]
impl<T: CoordNum> MultiLineStringTrait for MultiLineString<T> {
type T = T;
type LineStringType<'a> = &'a LineString<Self::T> where Self: 'a;
Expand All @@ -67,6 +69,7 @@ impl<T: CoordNum> MultiLineStringTrait for MultiLineString<T> {
}
}

#[cfg(feature = "geo-types")]
impl<'a, T: CoordNum> MultiLineStringTrait for &'a MultiLineString<T> {
type T = T;
type LineStringType<'b> = &'a LineString<Self::T> where Self: 'b;
Expand All @@ -88,9 +91,9 @@ impl<'a, T: CoordNum> MultiLineStringTrait for &'a MultiLineString<T> {
///
/// This can be used as the `MultiLineStringType` of the `GeometryTrait` by implementations that
/// don't have a MultiLineString concept
pub struct UnimplementedMultiLineString<T: CoordNum>(PhantomData<T>);
pub struct UnimplementedMultiLineString<T>(PhantomData<T>);

impl<T: CoordNum> MultiLineStringTrait for UnimplementedMultiLineString<T> {
impl<T> MultiLineStringTrait for UnimplementedMultiLineString<T> {
type T = T;
type LineStringType<'a> = UnimplementedLineString<Self::T> where Self: 'a;

Expand Down
9 changes: 6 additions & 3 deletions geo-traits/src/multi_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::marker::PhantomData;

use crate::iterator::MultiPointIterator;
use crate::{Dimensions, PointTrait, UnimplementedPoint};
#[cfg(feature = "geo-types")]
use geo_types::{CoordNum, MultiPoint, Point};

/// A trait for accessing data from a generic MultiPoint.
Expand All @@ -11,7 +12,7 @@ use geo_types::{CoordNum, MultiPoint, Point};
/// Refer to [geo_types::MultiPoint] for information about semantics and validity.
pub trait MultiPointTrait: Sized {
/// The coordinate type of this geometry
type T: CoordNum;
type T;

/// The type of each underlying Point, which implements [PointTrait]
type PointType<'a>: 'a + PointTrait<T = Self::T>
Expand Down Expand Up @@ -47,6 +48,7 @@ pub trait MultiPointTrait: Sized {
unsafe fn point_unchecked(&self, i: usize) -> Self::PointType<'_>;
}

#[cfg(feature = "geo-types")]
impl<T: CoordNum> MultiPointTrait for MultiPoint<T> {
type T = T;
type PointType<'a> = &'a Point<Self::T> where Self: 'a;
Expand All @@ -64,6 +66,7 @@ impl<T: CoordNum> MultiPointTrait for MultiPoint<T> {
}
}

#[cfg(feature = "geo-types")]
impl<'a, T: CoordNum> MultiPointTrait for &'a MultiPoint<T> {
type T = T;
type PointType<'b> = &'a Point<Self::T> where Self: 'b;
Expand All @@ -85,9 +88,9 @@ impl<'a, T: CoordNum> MultiPointTrait for &'a MultiPoint<T> {
///
/// This can be used as the `MultiPointType` of the `GeometryTrait` by implementations that don't
/// have a MultiPoint concept
pub struct UnimplementedMultiPoint<T: CoordNum>(PhantomData<T>);
pub struct UnimplementedMultiPoint<T>(PhantomData<T>);

impl<T: CoordNum> MultiPointTrait for UnimplementedMultiPoint<T> {
impl<T> MultiPointTrait for UnimplementedMultiPoint<T> {
type T = T;
type PointType<'a> = UnimplementedPoint<Self::T> where Self: 'a;

Expand Down
Loading

0 comments on commit ceca778

Please sign in to comment.