Skip to content

Commit 914639a

Browse files
committed
No alloc support for geo-types
1 parent 4ad7654 commit 914639a

File tree

8 files changed

+104
-22
lines changed

8 files changed

+104
-22
lines changed

geo-types/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ edition = "2021"
1111

1212
[features]
1313
default = ["std"]
14-
std = []
14+
alloc = []
15+
std = ["alloc"]
1516
# Prefer `use-rstar` feature rather than enabling rstar directly.
1617
# rstar integration relies on the optional approx crate, but implicit features cannot yet enable other features.
1718
# See: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#namespaced-features

geo-types/src/error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ impl fmt::Display for Error {
2121
}
2222
}
2323

24-
#[cfg(test)]
24+
// NOTE: Move feature = alloc to error_output if/when other tests that do not need alloc get added
25+
#[cfg(all(test, feature = "alloc"))]
2526
mod test {
2627
use crate::{Geometry, Point, Rect};
2728
use alloc::string::ToString;

geo-types/src/geometry/mod.rs

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
11
pub(crate) mod coord;
2-
pub(crate) mod geometry_collection;
32
pub(crate) mod line;
3+
pub(crate) mod point;
4+
pub(crate) mod rect;
5+
pub(crate) mod triangle;
6+
7+
#[cfg(feature = "alloc")]
8+
pub(crate) mod geometry_collection;
9+
#[cfg(feature = "alloc")]
410
pub(crate) mod line_string;
11+
#[cfg(feature = "alloc")]
512
pub(crate) mod multi_line_string;
13+
#[cfg(feature = "alloc")]
614
pub(crate) mod multi_point;
15+
#[cfg(feature = "alloc")]
716
pub(crate) mod multi_polygon;
8-
pub(crate) mod point;
17+
#[cfg(feature = "alloc")]
918
pub(crate) mod polygon;
10-
pub(crate) mod rect;
11-
pub(crate) mod triangle;
1219

1320
// re-export all the geometry variants:
1421
#[allow(deprecated)]
1522
pub use coord::{Coord, Coordinate};
16-
pub use geometry_collection::GeometryCollection;
1723
pub use line::Line;
24+
pub use point::Point;
25+
pub use rect::Rect;
26+
pub use triangle::Triangle;
27+
28+
#[cfg(feature = "alloc")]
29+
pub use geometry_collection::GeometryCollection;
30+
#[cfg(feature = "alloc")]
1831
pub use line_string::LineString;
32+
#[cfg(feature = "alloc")]
1933
pub use multi_line_string::MultiLineString;
34+
#[cfg(feature = "alloc")]
2035
pub use multi_point::MultiPoint;
36+
#[cfg(feature = "alloc")]
2137
pub use multi_polygon::MultiPolygon;
22-
pub use point::Point;
38+
#[cfg(feature = "alloc")]
2339
pub use polygon::Polygon;
24-
pub use rect::Rect;
25-
pub use triangle::Triangle;
2640

27-
use crate::{CoordNum, Error};
41+
use crate::CoordNum;
42+
use crate::Error;
2843

2944
#[cfg(any(feature = "approx", test))]
3045
use approx::{AbsDiffEq, RelativeEq};
@@ -41,7 +56,7 @@ use core::convert::TryFrom;
4156
///
4257
/// ```
4358
/// use std::convert::TryFrom;
44-
/// use geo_types::{Point, point, Geometry, GeometryCollection};
59+
/// use geo_types::{Point, point, Geometry};
4560
/// let p = point!(x: 1.0, y: 1.0);
4661
/// let pe: Geometry = p.into();
4762
/// let pn = Point::try_from(pe).unwrap();
@@ -52,11 +67,17 @@ use core::convert::TryFrom;
5267
pub enum Geometry<T: CoordNum = f64> {
5368
Point(Point<T>),
5469
Line(Line<T>),
70+
#[cfg(feature = "alloc")]
5571
LineString(LineString<T>),
72+
#[cfg(feature = "alloc")]
5673
Polygon(Polygon<T>),
74+
#[cfg(feature = "alloc")]
5775
MultiPoint(MultiPoint<T>),
76+
#[cfg(feature = "alloc")]
5877
MultiLineString(MultiLineString<T>),
78+
#[cfg(feature = "alloc")]
5979
MultiPolygon(MultiPolygon<T>),
80+
#[cfg(feature = "alloc")]
6081
GeometryCollection(GeometryCollection<T>),
6182
Rect(Rect<T>),
6283
Triangle(Triangle<T>),
@@ -72,26 +93,31 @@ impl<T: CoordNum> From<Line<T>> for Geometry<T> {
7293
Self::Line(x)
7394
}
7495
}
96+
#[cfg(feature = "alloc")]
7597
impl<T: CoordNum> From<LineString<T>> for Geometry<T> {
7698
fn from(x: LineString<T>) -> Self {
7799
Self::LineString(x)
78100
}
79101
}
102+
#[cfg(feature = "alloc")]
80103
impl<T: CoordNum> From<Polygon<T>> for Geometry<T> {
81104
fn from(x: Polygon<T>) -> Self {
82105
Self::Polygon(x)
83106
}
84107
}
108+
#[cfg(feature = "alloc")]
85109
impl<T: CoordNum> From<MultiPoint<T>> for Geometry<T> {
86110
fn from(x: MultiPoint<T>) -> Self {
87111
Self::MultiPoint(x)
88112
}
89113
}
114+
#[cfg(feature = "alloc")]
90115
impl<T: CoordNum> From<MultiLineString<T>> for Geometry<T> {
91116
fn from(x: MultiLineString<T>) -> Self {
92117
Self::MultiLineString(x)
93118
}
94119
}
120+
#[cfg(feature = "alloc")]
95121
impl<T: CoordNum> From<MultiPolygon<T>> for Geometry<T> {
96122
fn from(x: MultiPolygon<T>) -> Self {
97123
Self::MultiPolygon(x)
@@ -145,6 +171,7 @@ impl<T: CoordNum> Geometry<T> {
145171
#[deprecated(
146172
note = "Will be removed in an upcoming version. Switch to std::convert::TryInto<LineString>"
147173
)]
174+
#[cfg(feature = "alloc")]
148175
pub fn into_line_string(self) -> Option<LineString<T>> {
149176
if let Geometry::LineString(x) = self {
150177
Some(x)
@@ -169,6 +196,7 @@ impl<T: CoordNum> Geometry<T> {
169196
#[deprecated(
170197
note = "Will be removed in an upcoming version. Switch to std::convert::TryInto<Polygon>"
171198
)]
199+
#[cfg(feature = "alloc")]
172200
pub fn into_polygon(self) -> Option<Polygon<T>> {
173201
if let Geometry::Polygon(x) = self {
174202
Some(x)
@@ -181,6 +209,7 @@ impl<T: CoordNum> Geometry<T> {
181209
#[deprecated(
182210
note = "Will be removed in an upcoming version. Switch to std::convert::TryInto<MultiPoint>"
183211
)]
212+
#[cfg(feature = "alloc")]
184213
pub fn into_multi_point(self) -> Option<MultiPoint<T>> {
185214
if let Geometry::MultiPoint(x) = self {
186215
Some(x)
@@ -193,6 +222,7 @@ impl<T: CoordNum> Geometry<T> {
193222
#[deprecated(
194223
note = "Will be removed in an upcoming version. Switch to std::convert::TryInto<MultiLineString>"
195224
)]
225+
#[cfg(feature = "alloc")]
196226
pub fn into_multi_line_string(self) -> Option<MultiLineString<T>> {
197227
if let Geometry::MultiLineString(x) = self {
198228
Some(x)
@@ -205,6 +235,7 @@ impl<T: CoordNum> Geometry<T> {
205235
#[deprecated(
206236
note = "Will be removed in an upcoming version. Switch to std::convert::TryInto<MultiPolygon>"
207237
)]
238+
#[cfg(feature = "alloc")]
208239
pub fn into_multi_polygon(self) -> Option<MultiPolygon<T>> {
209240
if let Geometry::MultiPolygon(x) = self {
210241
Some(x)
@@ -215,11 +246,12 @@ impl<T: CoordNum> Geometry<T> {
215246
}
216247

217248
macro_rules! try_from_geometry_impl {
218-
($($type: ident),+) => {
249+
($($(#[$attr:meta])* $type: ident),+) => {
219250
$(
220251
/// Convert a Geometry enum into its inner type.
221252
///
222253
/// Fails if the enum case does not match the type you are trying to convert it to.
254+
$(#[$attr])*
223255
impl <T: CoordNum> TryFrom<Geometry<T>> for $type<T> {
224256
type Error = Error;
225257

@@ -240,10 +272,15 @@ macro_rules! try_from_geometry_impl {
240272
try_from_geometry_impl!(
241273
Point,
242274
Line,
275+
#[cfg(feature = "alloc")]
243276
LineString,
277+
#[cfg(feature = "alloc")]
244278
Polygon,
279+
#[cfg(feature = "alloc")]
245280
MultiPoint,
281+
#[cfg(feature = "alloc")]
246282
MultiLineString,
283+
#[cfg(feature = "alloc")]
247284
MultiPolygon,
248285
// Disabled until we remove the deprecated GeometryCollection::from(single_geom) impl.
249286
// GeometryCollection,
@@ -258,11 +295,17 @@ where
258295
match geometry {
259296
Geometry::Point(_) => type_name::<Point<T>>(),
260297
Geometry::Line(_) => type_name::<Line<T>>(),
298+
#[cfg(feature = "alloc")]
261299
Geometry::LineString(_) => type_name::<LineString<T>>(),
300+
#[cfg(feature = "alloc")]
262301
Geometry::Polygon(_) => type_name::<Polygon<T>>(),
302+
#[cfg(feature = "alloc")]
263303
Geometry::MultiPoint(_) => type_name::<MultiPoint<T>>(),
304+
#[cfg(feature = "alloc")]
264305
Geometry::MultiLineString(_) => type_name::<MultiLineString<T>>(),
306+
#[cfg(feature = "alloc")]
265307
Geometry::MultiPolygon(_) => type_name::<MultiPolygon<T>>(),
308+
#[cfg(feature = "alloc")]
266309
Geometry::GeometryCollection(_) => type_name::<GeometryCollection<T>>(),
267310
Geometry::Rect(_) => type_name::<Rect<T>>(),
268311
Geometry::Triangle(_) => type_name::<Triangle<T>>(),
@@ -284,10 +327,10 @@ where
284327
/// # Examples
285328
///
286329
/// ```
287-
/// use geo_types::{Geometry, polygon};
330+
/// use geo_types::{Geometry, Line, point};
288331
///
289-
/// let a: Geometry<f32> = polygon![(x: 0., y: 0.), (x: 5., y: 0.), (x: 7., y: 9.), (x: 0., y: 0.)].into();
290-
/// let b: Geometry<f32> = polygon![(x: 0., y: 0.), (x: 5., y: 0.), (x: 7.01, y: 9.), (x: 0., y: 0.)].into();
332+
/// let a: Geometry<f32> = Line::new(point!(x: 0., y: 0.), point!(x: 7., y: 9.)).into();
333+
/// let b: Geometry<f32> = Line::new(point!(x: 0., y: 0.), point!(x: 7.01, y: 9.)).into();
291334
///
292335
/// approx::assert_relative_eq!(a, b, max_relative=0.1);
293336
/// approx::assert_relative_ne!(a, b, max_relative=0.001);
@@ -302,21 +345,27 @@ where
302345
match (self, other) {
303346
(Geometry::Point(g1), Geometry::Point(g2)) => g1.relative_eq(g2, epsilon, max_relative),
304347
(Geometry::Line(g1), Geometry::Line(g2)) => g1.relative_eq(g2, epsilon, max_relative),
348+
#[cfg(feature = "alloc")]
305349
(Geometry::LineString(g1), Geometry::LineString(g2)) => {
306350
g1.relative_eq(g2, epsilon, max_relative)
307351
}
352+
#[cfg(feature = "alloc")]
308353
(Geometry::Polygon(g1), Geometry::Polygon(g2)) => {
309354
g1.relative_eq(g2, epsilon, max_relative)
310355
}
356+
#[cfg(feature = "alloc")]
311357
(Geometry::MultiPoint(g1), Geometry::MultiPoint(g2)) => {
312358
g1.relative_eq(g2, epsilon, max_relative)
313359
}
360+
#[cfg(feature = "alloc")]
314361
(Geometry::MultiLineString(g1), Geometry::MultiLineString(g2)) => {
315362
g1.relative_eq(g2, epsilon, max_relative)
316363
}
364+
#[cfg(feature = "alloc")]
317365
(Geometry::MultiPolygon(g1), Geometry::MultiPolygon(g2)) => {
318366
g1.relative_eq(g2, epsilon, max_relative)
319367
}
368+
#[cfg(feature = "alloc")]
320369
(Geometry::GeometryCollection(g1), Geometry::GeometryCollection(g2)) => {
321370
g1.relative_eq(g2, epsilon, max_relative)
322371
}
@@ -343,10 +392,10 @@ impl<T: AbsDiffEq<Epsilon = T> + CoordNum> AbsDiffEq for Geometry<T> {
343392
/// # Examples
344393
///
345394
/// ```
346-
/// use geo_types::{Geometry, polygon};
395+
/// use geo_types::{Geometry, Line, point};
347396
///
348-
/// let a: Geometry<f32> = polygon![(x: 0., y: 0.), (x: 5., y: 0.), (x: 7., y: 9.), (x: 0., y: 0.)].into();
349-
/// let b: Geometry<f32> = polygon![(x: 0., y: 0.), (x: 5., y: 0.), (x: 7.01, y: 9.), (x: 0., y: 0.)].into();
397+
/// let a: Geometry<f32> = Line::new(point!(x: 0., y: 0.), point!(x: 7., y: 9.)).into();
398+
/// let b: Geometry<f32> = Line::new(point!(x: 0., y: 0.), point!(x: 7.01, y: 9.)).into();
350399
///
351400
/// approx::assert_abs_diff_eq!(a, b, epsilon=0.1);
352401
/// approx::assert_abs_diff_ne!(a, b, epsilon=0.001);
@@ -355,13 +404,19 @@ impl<T: AbsDiffEq<Epsilon = T> + CoordNum> AbsDiffEq for Geometry<T> {
355404
match (self, other) {
356405
(Geometry::Point(g1), Geometry::Point(g2)) => g1.abs_diff_eq(g2, epsilon),
357406
(Geometry::Line(g1), Geometry::Line(g2)) => g1.abs_diff_eq(g2, epsilon),
407+
#[cfg(feature = "alloc")]
358408
(Geometry::LineString(g1), Geometry::LineString(g2)) => g1.abs_diff_eq(g2, epsilon),
409+
#[cfg(feature = "alloc")]
359410
(Geometry::Polygon(g1), Geometry::Polygon(g2)) => g1.abs_diff_eq(g2, epsilon),
411+
#[cfg(feature = "alloc")]
360412
(Geometry::MultiPoint(g1), Geometry::MultiPoint(g2)) => g1.abs_diff_eq(g2, epsilon),
413+
#[cfg(feature = "alloc")]
361414
(Geometry::MultiLineString(g1), Geometry::MultiLineString(g2)) => {
362415
g1.abs_diff_eq(g2, epsilon)
363416
}
417+
#[cfg(feature = "alloc")]
364418
(Geometry::MultiPolygon(g1), Geometry::MultiPolygon(g2)) => g1.abs_diff_eq(g2, epsilon),
419+
#[cfg(feature = "alloc")]
365420
(Geometry::GeometryCollection(g1), Geometry::GeometryCollection(g2)) => {
366421
g1.abs_diff_eq(g2, epsilon)
367422
}

geo-types/src/geometry/rect.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{coord, polygon, Coord, CoordFloat, CoordNum, Line, Polygon};
1+
use crate::{coord, Coord, CoordFloat, CoordNum, Line};
2+
#[cfg(feature = "alloc")]
3+
use crate::{polygon, Polygon};
24

35
#[cfg(any(feature = "approx", test))]
46
use approx::{AbsDiffEq, RelativeEq};
@@ -214,6 +216,7 @@ impl<T: CoordNum> Rect<T> {
214216
/// ],
215217
/// );
216218
/// ```
219+
#[cfg(feature = "alloc")]
217220
pub fn to_polygon(self) -> Polygon<T> {
218221
polygon![
219222
(x: self.min.x, y: self.min.y),

geo-types/src/geometry/triangle.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{polygon, Coord, CoordNum, Line, Polygon};
1+
#[cfg(feature = "alloc")]
2+
use crate::{polygon, Polygon};
3+
use crate::{Coord, CoordNum, Line};
24

35
#[cfg(any(feature = "approx", test))]
46
use approx::{AbsDiffEq, RelativeEq};
@@ -52,6 +54,7 @@ impl<T: CoordNum> Triangle<T> {
5254
/// ],
5355
/// );
5456
/// ```
57+
#[cfg(feature = "alloc")]
5558
pub fn to_polygon(self) -> Polygon<T> {
5659
polygon![self.0, self.1, self.2, self.0]
5760
}

geo-types/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
//! [OGC-SFA]: https://www.ogc.org/standards/sfa
7777
//! [rstar]: https://github.com/Stoeoef/rstar
7878
//! [Serde]: https://serde.rs/
79+
#[cfg(feature = "alloc")]
7980
extern crate alloc;
8081
extern crate num_traits;
8182

@@ -116,6 +117,7 @@ impl<T: CoordNum + Float> CoordFloat for T {}
116117
pub mod geometry;
117118
pub use geometry::*;
118119

120+
#[cfg(feature = "alloc")]
119121
pub use geometry::line_string::PointsIter;
120122

121123
#[allow(deprecated)]
@@ -135,6 +137,7 @@ mod arbitrary;
135137
pub mod private_utils;
136138

137139
#[doc(hidden)]
140+
#[cfg(feature = "alloc")]
138141
pub mod _alloc {
139142
//! Needed to access these types from `alloc` in macros when the std feature is
140143
//! disabled and the calling context is missing `extern crate alloc`. These are
@@ -146,6 +149,7 @@ pub mod _alloc {
146149

147150
#[cfg(test)]
148151
mod tests {
152+
#[cfg(feature = "alloc")]
149153
use alloc::vec;
150154

151155
use super::*;
@@ -180,6 +184,7 @@ mod tests {
180184
}
181185

182186
#[test]
187+
#[cfg(feature = "alloc")]
183188
fn polygon_new_test() {
184189
let exterior = LineString::new(vec![
185190
coord! { x: 0., y: 0. },
@@ -200,6 +205,7 @@ mod tests {
200205
}
201206

202207
#[test]
208+
#[cfg(feature = "alloc")]
203209
fn iters() {
204210
let _: MultiPoint<_> = vec![(0., 0.), (1., 2.)].into();
205211
let _: MultiPoint<_> = vec![(0., 0.), (1., 2.)].into_iter().collect();
@@ -253,6 +259,7 @@ mod tests {
253259
}
254260

255261
#[test]
262+
#[cfg(feature = "alloc")]
256263
fn test_rects() {
257264
let r = Rect::new(coord! { x: -1., y: -1. }, coord! { x: 1., y: 1. });
258265
let p: Polygon<_> = r.into();

0 commit comments

Comments
 (0)