|
| 1 | +use std::marker::PhantomData; |
| 2 | + |
| 3 | +use geo_types::{Coord, CoordNum}; |
| 4 | + |
| 5 | +use crate::Dimensions; |
| 6 | + |
| 7 | +/// A trait for accessing data from a generic Coord. |
| 8 | +/// |
| 9 | +/// Refer to [geo_types::Coord] for information about semantics and validity. |
| 10 | +pub trait CoordTrait { |
| 11 | + /// The coordinate type of this geometry |
| 12 | + type T: CoordNum; |
| 13 | + |
| 14 | + /// Dimensions of the coordinate tuple |
| 15 | + fn dim(&self) -> Dimensions; |
| 16 | + |
| 17 | + /// Access the n'th (0-based) element of the CoordinateTuple. |
| 18 | + /// Returns `None` if `n >= DIMENSION`. |
| 19 | + /// See also [`nth_unchecked()`](Self::nth_unchecked). |
| 20 | + fn nth(&self, n: usize) -> Option<Self::T> { |
| 21 | + if n < self.dim().size() { |
| 22 | + Some(self.nth_unchecked(n)) |
| 23 | + } else { |
| 24 | + None |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /// x component of this coord. |
| 29 | + fn x(&self) -> Self::T; |
| 30 | + |
| 31 | + /// y component of this coord. |
| 32 | + fn y(&self) -> Self::T; |
| 33 | + |
| 34 | + /// Returns a tuple that contains the x/horizontal & y/vertical component of the coord. |
| 35 | + fn x_y(&self) -> (Self::T, Self::T) { |
| 36 | + (self.x(), self.y()) |
| 37 | + } |
| 38 | + |
| 39 | + /// Access the n'th (0-based) element of the CoordinateTuple. |
| 40 | + /// May panic if n >= DIMENSION. |
| 41 | + /// See also [`nth()`](Self::nth). |
| 42 | + fn nth_unchecked(&self, n: usize) -> Self::T; |
| 43 | +} |
| 44 | + |
| 45 | +impl<T: CoordNum> CoordTrait for Coord<T> { |
| 46 | + type T = T; |
| 47 | + |
| 48 | + fn nth_unchecked(&self, n: usize) -> Self::T { |
| 49 | + match n { |
| 50 | + 0 => self.x(), |
| 51 | + 1 => self.y(), |
| 52 | + _ => panic!("Coord only supports 2 dimensions"), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + fn dim(&self) -> Dimensions { |
| 57 | + Dimensions::Xy |
| 58 | + } |
| 59 | + |
| 60 | + fn x(&self) -> Self::T { |
| 61 | + self.x |
| 62 | + } |
| 63 | + |
| 64 | + fn y(&self) -> Self::T { |
| 65 | + self.y |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<T: CoordNum> CoordTrait for &Coord<T> { |
| 70 | + type T = T; |
| 71 | + |
| 72 | + fn nth_unchecked(&self, n: usize) -> Self::T { |
| 73 | + match n { |
| 74 | + 0 => self.x(), |
| 75 | + 1 => self.y(), |
| 76 | + _ => panic!("Coord only supports 2 dimensions"), |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + fn dim(&self) -> Dimensions { |
| 81 | + Dimensions::Xy |
| 82 | + } |
| 83 | + |
| 84 | + fn x(&self) -> Self::T { |
| 85 | + self.x |
| 86 | + } |
| 87 | + |
| 88 | + fn y(&self) -> Self::T { |
| 89 | + self.y |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl<T: CoordNum> CoordTrait for (T, T) { |
| 94 | + type T = T; |
| 95 | + |
| 96 | + fn nth_unchecked(&self, n: usize) -> Self::T { |
| 97 | + match n { |
| 98 | + 0 => self.x(), |
| 99 | + 1 => self.y(), |
| 100 | + _ => panic!("(T, T) only supports 2 dimensions"), |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + fn dim(&self) -> Dimensions { |
| 105 | + Dimensions::Xy |
| 106 | + } |
| 107 | + |
| 108 | + fn x(&self) -> Self::T { |
| 109 | + self.0 |
| 110 | + } |
| 111 | + |
| 112 | + fn y(&self) -> Self::T { |
| 113 | + self.1 |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/// An empty struct that implements [CoordTrait]. |
| 118 | +/// |
| 119 | +/// This can be used as the `CoordType` of the `GeometryTrait` by implementations that don't have a |
| 120 | +/// Coord concept |
| 121 | +pub struct UnimplementedCoord<T: CoordNum>(PhantomData<T>); |
| 122 | + |
| 123 | +impl<T: CoordNum> CoordTrait for UnimplementedCoord<T> { |
| 124 | + type T = T; |
| 125 | + |
| 126 | + fn dim(&self) -> Dimensions { |
| 127 | + unimplemented!() |
| 128 | + } |
| 129 | + |
| 130 | + fn nth_unchecked(&self, _n: usize) -> Self::T { |
| 131 | + unimplemented!() |
| 132 | + } |
| 133 | + |
| 134 | + fn x(&self) -> Self::T { |
| 135 | + unimplemented!() |
| 136 | + } |
| 137 | + |
| 138 | + fn y(&self) -> Self::T { |
| 139 | + unimplemented!() |
| 140 | + } |
| 141 | +} |
0 commit comments