1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use geo::{Coord, Point};

pub trait PointTrait: Send + Sync {
    /// x component of this point
    fn x(&self) -> f64;

    /// y component of this point
    fn y(&self) -> f64;

    /// Returns a tuple that contains the x/horizontal & y/vertical component of the point.
    fn x_y(&self) -> (f64, f64);
}

impl PointTrait for Point<f64> {
    fn x(&self) -> f64 {
        self.0.x
    }

    fn y(&self) -> f64 {
        self.0.y
    }

    fn x_y(&self) -> (f64, f64) {
        (self.0.x, self.0.y)
    }
}

impl PointTrait for &Point<f64> {
    fn x(&self) -> f64 {
        self.0.x
    }

    fn y(&self) -> f64 {
        self.0.y
    }

    fn x_y(&self) -> (f64, f64) {
        (self.0.x, self.0.y)
    }
}

impl PointTrait for Coord<f64> {
    fn x(&self) -> f64 {
        self.x
    }

    fn y(&self) -> f64 {
        self.y
    }

    fn x_y(&self) -> (f64, f64) {
        (self.x, self.y)
    }
}

impl PointTrait for &Coord<f64> {
    fn x(&self) -> f64 {
        self.x
    }

    fn y(&self) -> f64 {
        self.y
    }

    fn x_y(&self) -> (f64, f64) {
        (self.x, self.y)
    }
}

#[cfg(test)]
mod tests {
    use super::PointTrait;
    use crate::array::PointArray;
    use crate::GeometryArrayTrait;

    #[test]
    fn test_point_function_geo() {
        fn identity(point: &impl PointTrait) -> &impl PointTrait {
            point
        }

        let point = geo::point!(x: 1., y: 2.);
        let output = identity(&point);

        assert_eq!(point.x_y(), output.x_y());

        let arrow_point_array: PointArray = vec![point].into();
        let arrow_point_scalar = &arrow_point_array.get(0).unwrap();
        let output_arrow_point_scalar = identity(arrow_point_scalar);

        assert_eq!(arrow_point_scalar.x(), output_arrow_point_scalar.x());
    }
}