Skip to content

Commit 81f1e05

Browse files
committed
Deprecate pub geo-type fields, add getters/setters
plus some other methods to help migrate from direct field access. This is intended to be a non-breaking change for now, to give people an upgrade window. In an upcoming *breaking* release of geo-types we'll drop pub field access altogether. This is in pursuit of adding support for 3D/4D geometries. We'll leverage generics in a way that is intended to avoid runtime cost for our mostly 2D user base. See #5 for more. This commit includes a bunch of new methods that correspond to the deprecated field access. See geo-types/CHANGES.md for a summary. `#[inline]` hints were added to maintain performance (it's actually improved in some places!) geo was updated to address all the deprecations.
1 parent 68f6e84 commit 81f1e05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1337
-829
lines changed

geo-postgis/src/to_postgis.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub trait ToPostgis<T> {
2121

2222
impl ToPostgis<ewkb::Point> for Coordinate<f64> {
2323
fn to_postgis_with_srid(&self, srid: Option<i32>) -> ewkb::Point {
24-
ewkb::Point::new(self.x, self.y, srid)
24+
ewkb::Point::new(self.x(), self.y(), srid)
2525
}
2626
}
2727

@@ -52,11 +52,7 @@ macro_rules! to_postgis_impl {
5252
($from:ident, $to:path, $name:ident) => {
5353
impl ToPostgis<$to> for $from<f64> {
5454
fn to_postgis_with_srid(&self, srid: Option<i32>) -> $to {
55-
let $name = self
56-
.0
57-
.iter()
58-
.map(|x| x.to_postgis_with_srid(srid))
59-
.collect();
55+
let $name = self.iter().map(|x| x.to_postgis_with_srid(srid)).collect();
6056
$to { $name, srid }
6157
}
6258
}
@@ -66,7 +62,17 @@ to_postgis_impl!(GeometryCollection, ewkb::GeometryCollection, geometries);
6662
to_postgis_impl!(MultiPolygon, ewkb::MultiPolygon, polygons);
6763
to_postgis_impl!(MultiLineString, ewkb::MultiLineString, lines);
6864
to_postgis_impl!(MultiPoint, ewkb::MultiPoint, points);
69-
to_postgis_impl!(LineString, ewkb::LineString, points);
65+
66+
impl ToPostgis<ewkb::LineString> for LineString<f64> {
67+
fn to_postgis_with_srid(&self, srid: Option<i32>) -> ewkb::LineString {
68+
let points = self
69+
.coords()
70+
.map(|x| x.to_postgis_with_srid(srid))
71+
.collect();
72+
ewkb::LineString { points, srid }
73+
}
74+
}
75+
7076
impl ToPostgis<ewkb::Geometry> for Geometry<f64> {
7177
fn to_postgis_with_srid(&self, srid: Option<i32>) -> ewkb::Geometry {
7278
match *self {

geo-types/CHANGES.md

+52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
# Changes
22

3+
## UNRELEASED
4+
5+
* DEPRECATED: Direct access to geometry fields (this is a big change).
6+
7+
This is intended to be a non-breaking change for now, to give people an
8+
upgrade window. In an upcoming *breaking* release of geo-types we'll drop pub
9+
field access altogether.
10+
11+
This is in pursuit of adding support for 3D/4D geometries. We'll leverage
12+
generics in a way that is intended to avoid runtime cost for our mostly 2D
13+
user base. See https://github.com/georust/geo/issues/5 for more.
14+
15+
ADDED: A bunch of new methods that correspond to the deprecated field access.
16+
Here's a cheatsheet of new methods, and a reminder of few existing methods
17+
which you might now need:
18+
* Coordinate:
19+
- `Coordinate::new(x, y)`
20+
- `coord.x()` / `coord.x_mut()`
21+
- `coord.y()` / `coord.y_mut()`
22+
* Point:
23+
- `point.coord()` / `point.coord_mut()`
24+
- `point.x_mut()`
25+
- `point.y_mut()`
26+
- `point.x()` / `point.y()` are not new, but you might need them now.
27+
* GeometryCollection:
28+
- `GeometryCollection::from(geometry_vec)`
29+
- `gc.geometries()` / `gc.geometries_mut()`
30+
- `gc.push(geometry)`: add a Geometry
31+
- `gc.into_inner()`: decompose into an owned Vec<Geometry>
32+
* Line:
33+
- `line.start()` / `line.start_mut()`
34+
- `line.end()` / `line.end_mut()`
35+
* LineString:
36+
- `line_string.inner()` / `line_string.inner_mut()`
37+
- `line_string.push(coord)`
38+
- `line_string[2]` get a particular coord. This isn't new, but you might need it now.
39+
- `line_string[0..2]` - you can now access a slice of coords too.
40+
* MultiPoint:
41+
- `multi_point.points()` / `multi_point.points_mut()`
42+
- `multi_point.push(point)` - add a point
43+
- `multi_point.into_inner()` - decompose into an owned Vec<Point>
44+
* MultiLineString:
45+
- `mls.line_strings()` / `mls.line_strings_mut()`
46+
- `mls.push(line_string)` - add a LineString
47+
- `mls.into_inner()`: decompose into an owned Vec<LineString>
48+
* MultiPolygon:
49+
- `multi_poly.polygons()` / `multi_poly.polygons_mut()`
50+
- `multi_poly.push(polygon)`: add a Polygon
51+
- `multi_poly.into_inner()`: decompose into an owned Vec<Polygon>
52+
53+
Something missing? Let us know! https://github.com/georust/geo/issues/new
54+
355
## 0.7.4
456

557
* BREAKING: Make `Rect::to_lines` return lines in winding order for `Rect::to_polygon`.

geo-types/src/coordinate.rs

+85-39
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,55 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq};
2626
#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash, Default)]
2727
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2828
pub struct Coordinate<T: CoordNum> {
29+
#[deprecated(
30+
since = "0.7.5",
31+
note = "Direct field access is deprecated - use `coord.x()` or `coord.x_mut()` for field access and `coord!(x: 1, y: 2)` or `Coordinate::new(x, y)` for construction"
32+
)]
2933
pub x: T,
34+
#[deprecated(
35+
since = "0.7.5",
36+
note = "Direct field access is deprecated - use `coord.y()` or `coord.y_mut()` for field access and `coord!(x: 1, y: 2)` or `Coordinate::new(x, y)` for construction"
37+
)]
3038
pub y: T,
3139
}
3240

41+
impl<T: CoordNum> Coordinate<T> {
42+
#[inline]
43+
pub fn new(x: T, y: T) -> Self {
44+
// we can delete this `allow(deprecated)` once the fields are no longer pub
45+
#[allow(deprecated)]
46+
Self { x, y }
47+
}
48+
49+
#[inline]
50+
pub fn x(&self) -> T {
51+
// we can delete this `allow(deprecated)` once the fields are no longer pub
52+
#[allow(deprecated)]
53+
self.x
54+
}
55+
56+
#[inline]
57+
pub fn x_mut(&mut self) -> &mut T {
58+
// we can delete this `allow(deprecated)` once the fields are no longer pub
59+
#[allow(deprecated)]
60+
&mut self.x
61+
}
62+
63+
#[inline]
64+
pub fn y(&self) -> T {
65+
// we can delete this `allow(deprecated)` once the fields are no longer pub
66+
#[allow(deprecated)]
67+
self.y
68+
}
69+
70+
#[inline]
71+
pub fn y_mut(&mut self) -> &mut T {
72+
// we can delete this `allow(deprecated)` once the fields are no longer pub
73+
#[allow(deprecated)]
74+
&mut self.y
75+
}
76+
}
77+
3378
impl<T: CoordNum> From<(T, T)> for Coordinate<T> {
3479
#[inline]
3580
fn from(coords: (T, T)) -> Self {
@@ -63,14 +108,14 @@ impl<T: CoordNum> From<Point<T>> for Coordinate<T> {
63108
impl<T: CoordNum> From<Coordinate<T>> for (T, T) {
64109
#[inline]
65110
fn from(coord: Coordinate<T>) -> Self {
66-
(coord.x, coord.y)
111+
(coord.x(), coord.y())
67112
}
68113
}
69114

70115
impl<T: CoordNum> From<Coordinate<T>> for [T; 2] {
71116
#[inline]
72117
fn from(coord: Coordinate<T>) -> Self {
73-
[coord.x, coord.y]
118+
[coord.x(), coord.y()]
74119
}
75120
}
76121

@@ -93,7 +138,7 @@ impl<T: CoordNum> Coordinate<T> {
93138
/// ```
94139
#[inline]
95140
pub fn x_y(&self) -> (T, T) {
96-
(self.x, self.y)
141+
(self.x(), self.y())
97142
}
98143
}
99144

@@ -109,8 +154,8 @@ use std::ops::{Add, Div, Mul, Neg, Sub};
109154
/// let p = coord! { x: 1.25, y: 2.5 };
110155
/// let q = -p;
111156
///
112-
/// assert_eq!(q.x, -p.x);
113-
/// assert_eq!(q.y, -p.y);
157+
/// assert_eq!(q.x(), -p.x());
158+
/// assert_eq!(q.y(), -p.y());
114159
/// ```
115160
impl<T> Neg for Coordinate<T>
116161
where
@@ -121,8 +166,8 @@ where
121166
#[inline]
122167
fn neg(self) -> Self {
123168
coord! {
124-
x: -self.x,
125-
y: -self.y,
169+
x: -self.x(),
170+
y: -self.y(),
126171
}
127172
}
128173
}
@@ -138,17 +183,17 @@ where
138183
/// let q = coord! { x: 1.5, y: 2.5 };
139184
/// let sum = p + q;
140185
///
141-
/// assert_eq!(sum.x, 2.75);
142-
/// assert_eq!(sum.y, 5.0);
186+
/// assert_eq!(sum.x(), 2.75);
187+
/// assert_eq!(sum.y(), 5.0);
143188
/// ```
144189
impl<T: CoordNum> Add for Coordinate<T> {
145190
type Output = Self;
146191

147192
#[inline]
148193
fn add(self, rhs: Self) -> Self {
149194
coord! {
150-
x: self.x + rhs.x,
151-
y: self.y + rhs.y,
195+
x: self.x() + rhs.x(),
196+
y: self.y() + rhs.y(),
152197
}
153198
}
154199
}
@@ -164,17 +209,17 @@ impl<T: CoordNum> Add for Coordinate<T> {
164209
/// let q = coord! { x: 1.25, y: 2.5 };
165210
/// let diff = p - q;
166211
///
167-
/// assert_eq!(diff.x, 0.25);
168-
/// assert_eq!(diff.y, 0.);
212+
/// assert_eq!(diff.x(), 0.25);
213+
/// assert_eq!(diff.y(), 0.);
169214
/// ```
170215
impl<T: CoordNum> Sub for Coordinate<T> {
171216
type Output = Self;
172217

173218
#[inline]
174219
fn sub(self, rhs: Self) -> Self {
175220
coord! {
176-
x: self.x - rhs.x,
177-
y: self.y - rhs.y,
221+
x: self.x() - rhs.x(),
222+
y: self.y() - rhs.y(),
178223
}
179224
}
180225
}
@@ -189,17 +234,17 @@ impl<T: CoordNum> Sub for Coordinate<T> {
189234
/// let p = coord! { x: 1.25, y: 2.5 };
190235
/// let q = p * 4.;
191236
///
192-
/// assert_eq!(q.x, 5.0);
193-
/// assert_eq!(q.y, 10.0);
237+
/// assert_eq!(q.x(), 5.0);
238+
/// assert_eq!(q.y(), 10.0);
194239
/// ```
195240
impl<T: CoordNum> Mul<T> for Coordinate<T> {
196241
type Output = Self;
197242

198243
#[inline]
199244
fn mul(self, rhs: T) -> Self {
200245
coord! {
201-
x: self.x * rhs,
202-
y: self.y * rhs,
246+
x: self.x() * rhs,
247+
y: self.y() * rhs,
203248
}
204249
}
205250
}
@@ -214,17 +259,17 @@ impl<T: CoordNum> Mul<T> for Coordinate<T> {
214259
/// let p = coord! { x: 5., y: 10. };
215260
/// let q = p / 4.;
216261
///
217-
/// assert_eq!(q.x, 1.25);
218-
/// assert_eq!(q.y, 2.5);
262+
/// assert_eq!(q.x(), 1.25);
263+
/// assert_eq!(q.y(), 2.5);
219264
/// ```
220265
impl<T: CoordNum> Div<T> for Coordinate<T> {
221266
type Output = Self;
222267

223268
#[inline]
224269
fn div(self, rhs: T) -> Self {
225270
coord! {
226-
x: self.x / rhs,
227-
y: self.y / rhs,
271+
x: self.x() / rhs,
272+
y: self.y() / rhs,
228273
}
229274
}
230275
}
@@ -240,8 +285,8 @@ use num_traits::Zero;
240285
///
241286
/// let p: Coordinate<f64> = Zero::zero();
242287
///
243-
/// assert_eq!(p.x, 0.);
244-
/// assert_eq!(p.y, 0.);
288+
/// assert_eq!(p.x(), 0.);
289+
/// assert_eq!(p.y(), 0.);
245290
/// ```
246291
impl<T: CoordNum> Coordinate<T> {
247292
#[inline]
@@ -260,7 +305,7 @@ impl<T: CoordNum> Zero for Coordinate<T> {
260305
}
261306
#[inline]
262307
fn is_zero(&self) -> bool {
263-
self.x.is_zero() && self.y.is_zero()
308+
self.x().is_zero() && self.y().is_zero()
264309
}
265310
}
266311

@@ -278,7 +323,8 @@ where
278323

279324
#[inline]
280325
fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
281-
T::abs_diff_eq(&self.x, &other.x, epsilon) && T::abs_diff_eq(&self.y, &other.y, epsilon)
326+
T::abs_diff_eq(&self.x(), &other.x(), epsilon)
327+
&& T::abs_diff_eq(&self.y(), &other.y(), epsilon)
282328
}
283329
}
284330

@@ -294,8 +340,8 @@ where
294340

295341
#[inline]
296342
fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
297-
T::relative_eq(&self.x, &other.x, epsilon, max_relative)
298-
&& T::relative_eq(&self.y, &other.y, epsilon, max_relative)
343+
T::relative_eq(&self.x(), &other.x(), epsilon, max_relative)
344+
&& T::relative_eq(&self.y(), &other.y(), epsilon, max_relative)
299345
}
300346
}
301347

@@ -311,8 +357,8 @@ where
311357

312358
#[inline]
313359
fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
314-
T::ulps_eq(&self.x, &other.x, epsilon, max_ulps)
315-
&& T::ulps_eq(&self.y, &other.y, epsilon, max_ulps)
360+
T::ulps_eq(&self.x(), &other.x(), epsilon, max_ulps)
361+
&& T::ulps_eq(&self.y(), &other.y(), epsilon, max_ulps)
316362
}
317363
}
318364

@@ -336,17 +382,17 @@ where
336382
#[inline]
337383
fn nth(&self, index: usize) -> Self::Scalar {
338384
match index {
339-
0 => self.x,
340-
1 => self.y,
385+
0 => self.x(),
386+
1 => self.y(),
341387
_ => unreachable!(),
342388
}
343389
}
344390

345391
#[inline]
346392
fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
347393
match index {
348-
0 => &mut self.x,
349-
1 => &mut self.y,
394+
0 => self.x_mut(),
395+
1 => self.y_mut(),
350396
_ => unreachable!(),
351397
}
352398
}
@@ -372,17 +418,17 @@ where
372418
#[inline]
373419
fn nth(&self, index: usize) -> Self::Scalar {
374420
match index {
375-
0 => self.x,
376-
1 => self.y,
421+
0 => self.x(),
422+
1 => self.y(),
377423
_ => unreachable!(),
378424
}
379425
}
380426

381427
#[inline]
382428
fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
383429
match index {
384-
0 => &mut self.x,
385-
1 => &mut self.y,
430+
0 => self.x_mut(),
431+
1 => self.y_mut(),
386432
_ => unreachable!(),
387433
}
388434
}

0 commit comments

Comments
 (0)