From 47c5adfb32370bac925a33e3b8e3369017c1191c Mon Sep 17 00:00:00 2001 From: Evan Carroll Date: Sun, 1 May 2022 18:48:08 -0500 Subject: [PATCH 1/2] Derive Default for all Point types --- src/ewkb.rs | 8 ++++---- src/twkb.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ewkb.rs b/src/ewkb.rs index ae325d6..5a921bb 100644 --- a/src/ewkb.rs +++ b/src/ewkb.rs @@ -15,14 +15,14 @@ use std::slice::Iter; // --- Structs for reading PostGIS geometries into -#[derive(PartialEq, Clone, Copy, Debug)] +#[derive(PartialEq, Clone, Copy, Debug, Default)] pub struct Point { pub x: f64, pub y: f64, pub srid: Option, } -#[derive(PartialEq, Clone, Copy, Debug)] +#[derive(PartialEq, Clone, Copy, Debug, Default)] pub struct PointZ { pub x: f64, pub y: f64, @@ -30,7 +30,7 @@ pub struct PointZ { pub srid: Option, } -#[derive(PartialEq, Clone, Copy, Debug)] +#[derive(PartialEq, Clone, Copy, Debug, Default)] pub struct PointM { pub x: f64, pub y: f64, @@ -38,7 +38,7 @@ pub struct PointM { pub srid: Option, } -#[derive(PartialEq, Clone, Copy, Debug)] +#[derive(PartialEq, Clone, Copy, Debug, Default)] pub struct PointZM { pub x: f64, pub y: f64, diff --git a/src/twkb.rs b/src/twkb.rs index 9e9faf2..a8a15a4 100644 --- a/src/twkb.rs +++ b/src/twkb.rs @@ -24,7 +24,7 @@ use std::io::prelude::*; use std::slice::Iter; use std::u8; -#[derive(PartialEq, Clone, Copy, Debug)] +#[derive(PartialEq, Clone, Copy, Debug, Default)] pub struct Point { pub x: f64, pub y: f64, // TODO: support for z, m From 734447f53ddac7d8078ef557d24e2bdd073d89d1 Mon Sep 17 00:00:00 2001 From: Evan Carroll Date: Sun, 1 May 2022 19:48:33 -0500 Subject: [PATCH 2/2] Implemented From for respective point types --- src/ewkb.rs | 24 ++++++++++++++++++++++++ src/twkb.rs | 8 +++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ewkb.rs b/src/ewkb.rs index 5a921bb..147d5e9 100644 --- a/src/ewkb.rs +++ b/src/ewkb.rs @@ -185,6 +185,12 @@ impl Point { } } +impl From<(f64, f64)> for Point { + fn from((x, y): (f64, f64)) -> Self { + Self::new(x, y, None) + } +} + impl postgis::Point for Point { fn x(&self) -> f64 { self.x @@ -214,6 +220,12 @@ impl PointZ { } } +impl From<(f64, f64, f64)> for PointZ { + fn from((x, y, z): (f64, f64, f64)) -> Self { + Self::new(x, y, z, None) + } +} + impl postgis::Point for PointZ { fn x(&self) -> f64 { self.x @@ -246,6 +258,12 @@ impl PointM { } } +impl From<(f64, f64, f64)> for PointM { + fn from((x, y, m): (f64, f64, f64)) -> Self { + Self::new(x, y, m, None) + } +} + impl postgis::Point for PointM { fn x(&self) -> f64 { self.x @@ -279,6 +297,12 @@ impl PointZM { } } +impl From<(f64, f64, f64, f64)> for PointZM { + fn from((x, y, z, m): (f64, f64, f64, f64)) -> Self { + Self::new(x, y, z, m, None) + } +} + impl postgis::Point for PointZM { fn x(&self) -> f64 { self.x diff --git a/src/twkb.rs b/src/twkb.rs index a8a15a4..4254076 100644 --- a/src/twkb.rs +++ b/src/twkb.rs @@ -200,7 +200,13 @@ fn read_varint64_as_f64(raw: &mut R, precision: i8) -> Result, _m: Option) -> Self { - Point { x: x, y: y } + Self { x: x, y: y } + } +} + +impl From<(f64, f64)> for Point { + fn from((x, y): (f64, f64)) -> Self { + Self { x, y } } }