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

wkt macro to create geo-types #1063

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 8 additions & 22 deletions geo-types/src/geometry/multi_line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,46 +197,32 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::line_string;
use crate::{line_string, wkt};

#[test]
fn test_iter() {
let multi: Vec<LineString<i32>> = vec![
line_string![(x: 0, y: 0), (x: 2, y: 0), (x: 1, y: 2), (x:0, y:0)],
line_string![(x: 10, y: 10), (x: 12, y: 10), (x: 11, y: 12), (x:10, y:10)],
];
let multi: MultiLineString<i32> = MultiLineString::new(multi);
let multi: MultiLineString<i32> = wkt! {
MULTILINESTRING((0 0,2 0,1 2,0 0), (10 10,12 10,11 12,10 10))
};

let mut first = true;
for p in &multi {
if first {
assert_eq!(
p,
&line_string![(x: 0, y: 0), (x: 2, y: 0), (x: 1, y: 2), (x:0, y:0)]
);
assert_eq!(p, &wkt! { LINESTRING(0 0,2 0,1 2,0 0) });
first = false;
} else {
assert_eq!(
p,
&line_string![(x: 10, y: 10), (x: 12, y: 10), (x: 11, y: 12), (x:10, y:10)]
);
assert_eq!(p, &wkt! { LINESTRING(10 10,12 10,11 12,10 10) });
}
}

// Do it again to prove that `multi` wasn't `moved`.
first = true;
for p in &multi {
if first {
assert_eq!(
p,
&line_string![(x: 0, y: 0), (x: 2, y: 0), (x: 1, y: 2), (x:0, y:0)]
);
assert_eq!(p, &wkt! { LINESTRING(0 0,2 0,1 2,0 0) });
first = false;
} else {
assert_eq!(
p,
&line_string![(x: 10, y: 10), (x: 12, y: 10), (x: 11, y: 12), (x:10, y:10)]
);
assert_eq!(p, &wkt! { LINESTRING(10 10,12 10,11 12,10 10) });
}
}
}
Expand Down
39 changes: 18 additions & 21 deletions geo-types/src/geometry/multi_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::point;
use crate::{point, wkt};

#[test]
fn test_iter() {
let multi = MultiPoint::new(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);
let multi = wkt! { MULTIPOINT(0 0,10 10) };

let mut first = true;
for p in &multi {
Expand All @@ -208,7 +208,7 @@ mod test {

#[test]
fn test_iter_mut() {
let mut multi = MultiPoint::new(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);
let mut multi = wkt! { MULTIPOINT(0 0,10 10) };

for point in &mut multi {
point.0.x += 1;
Expand All @@ -235,53 +235,50 @@ mod test {
fn test_relative_eq() {
let delta = 1e-6;

let multi = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
let multi = wkt! { MULTIPOINT(0. 0.,10. 10.) };

let mut multi_x = multi.clone();
*multi_x.0[0].x_mut() += delta;

let multi_x = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10.+delta, y: 10.]]);
assert!(multi.relative_eq(&multi_x, 1e-2, 1e-2));
assert!(multi.relative_ne(&multi_x, 1e-12, 1e-12));

let multi_y = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.+delta]]);
let mut multi_y = multi.clone();
*multi_y.0[0].y_mut() += delta;
assert!(multi.relative_eq(&multi_y, 1e-2, 1e-2));
assert!(multi.relative_ne(&multi_y, 1e-12, 1e-12));

// Under-sized but otherwise equal.
let multi_undersized = MultiPoint::new(vec![point![x: 0., y: 0.]]);
let multi_undersized = wkt! { MULTIPOINT(0. 0.) };
assert!(multi.relative_ne(&multi_undersized, 1., 1.));

// Over-sized but otherwise equal.
let multi_oversized = MultiPoint::new(vec![
point![x: 0., y: 0.],
point![x: 10., y: 10.],
point![x: 10., y:100.],
]);
let multi_oversized = wkt! { MULTIPOINT(0. 0.,10. 10.,10. 100.) };
assert!(multi.relative_ne(&multi_oversized, 1., 1.));
}

#[test]
fn test_abs_diff_eq() {
let delta = 1e-6;

let multi = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
let multi = wkt! { MULTIPOINT(0. 0.,10. 10.) };

let multi_x = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10.+delta, y: 10.]]);
let mut multi_x = multi.clone();
*multi_x.0[0].x_mut() += delta;
Copy link
Member Author

@michaelkirk michaelkirk Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example of where it's less nice. Unlike the serde_json::json! macro, you can't embed expressions. The wkt! macro only accepts literals.

assert!(multi.abs_diff_eq(&multi_x, 1e-2));
assert!(multi.abs_diff_ne(&multi_x, 1e-12));

let multi_y = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.+delta]]);
let mut multi_y = multi.clone();
*multi_y.0[0].y_mut() += delta;
assert!(multi.abs_diff_eq(&multi_y, 1e-2));
assert!(multi.abs_diff_ne(&multi_y, 1e-12));

// Under-sized but otherwise equal.
let multi_undersized = MultiPoint::new(vec![point![x: 0., y: 0.]]);
let multi_undersized = wkt! { MULTIPOINT(0. 0.) };
assert!(multi.abs_diff_ne(&multi_undersized, 1.));

// Over-sized but otherwise equal.
let multi_oversized = MultiPoint::new(vec![
point![x: 0., y: 0.],
point![x: 10., y: 10.],
point![x: 10., y:100.],
]);
let multi_oversized = wkt! { MULTIPOINT(0. 0.,10. 10.,10. 100.) };
assert!(multi.abs_diff_ne(&multi_oversized, 1.));
}
}
5 changes: 3 additions & 2 deletions geo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ pub use error::Error;
#[macro_use]
mod macros;

#[macro_use]
mod wkt_macro;

#[cfg(feature = "arbitrary")]
mod arbitrary;

Expand All @@ -147,8 +150,6 @@ pub mod _alloc {
//! Needed to access these types from `alloc` in macros when the std feature is
//! disabled and the calling context is missing `extern crate alloc`. These are
//! _not_ meant for public use.

pub use ::alloc::boxed::Box;
pub use ::alloc::vec;
}

Expand Down
26 changes: 10 additions & 16 deletions geo-types/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ macro_rules! coord {
/// [`LineString`]: ./line_string/struct.LineString.html
#[macro_export]
macro_rules! line_string {
() => { $crate::LineString::new(vec![]) };
() => { $crate::LineString::new($crate::_alloc::vec![]) };
(
$(( $($tag:tt : $val:expr),* $(,)? )),*
$(,)?
Expand All @@ -139,11 +139,9 @@ macro_rules! line_string {
$(,)?
) => {
$crate::LineString::new(
<[_]>::into_vec(
$crate::_alloc::Box::new(
[$($coord), *]
)
)
$crate::_alloc::vec![
$($coord),*
]
)
};
}
Expand Down Expand Up @@ -216,7 +214,7 @@ macro_rules! line_string {
/// [`Polygon`]: ./struct.Polygon.html
#[macro_export]
macro_rules! polygon {
() => { $crate::Polygon::new($crate::line_string![], vec![]) };
() => { $crate::Polygon::new($crate::line_string![], $crate::_alloc::vec![]) };
(
exterior: [
$(( $($exterior_tag:tt : $exterior_val:expr),* $(,)? )),*
Expand Down Expand Up @@ -262,15 +260,11 @@ macro_rules! polygon {
$crate::line_string![
$($exterior_coord), *
],
<[_]>::into_vec(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the nostd build, we need to give a fully qualified path to the vec macro. That's the primary change in this commit.

But also I noticed these into_vec calls. I don't think we were gaining anything by building an array and then boxing it into a Vec vs. building the vec! directly, so I've changed it.

$crate::_alloc::Box::new(
[
$(
$crate::line_string![$($interior_coord),*]
), *
]
)
)
$crate::_alloc::vec![
$(
$crate::line_string![$($interior_coord),*]
), *
]
)
};
(
Expand Down
Loading
Loading