geo_traits/
line.rs

1use std::marker::PhantomData;
2
3use crate::{CoordTrait, GeometryTrait, UnimplementedCoord};
4#[cfg(feature = "geo-types")]
5use geo_types::{Coord, CoordNum, Line};
6
7/// A trait for accessing data from a generic Line.
8///
9/// A Line is a line segment made up of exactly two [coordinates][CoordTrait].
10///
11/// Refer to [geo_types::Line] for information about semantics and validity.
12pub trait LineTrait: Sized + GeometryTrait {
13    /// The type of each underlying coordinate, which implements [CoordTrait]
14    type CoordType<'a>: 'a + CoordTrait<T = Self::T>
15    where
16        Self: 'a;
17
18    /// Access the start coordinate in this Line
19    fn start(&self) -> Self::CoordType<'_>;
20
21    /// Access the start coordinate in this Line
22    fn end(&self) -> Self::CoordType<'_>;
23
24    /// Access the two underlying coordinates
25    fn coords(&self) -> [Self::CoordType<'_>; 2] {
26        [self.start(), self.end()]
27    }
28}
29
30#[cfg(feature = "geo-types")]
31impl<T: CoordNum> LineTrait for Line<T> {
32    type CoordType<'a>
33        = &'a Coord<Self::T>
34    where
35        Self: 'a;
36
37    fn start(&self) -> Self::CoordType<'_> {
38        &self.start
39    }
40
41    fn end(&self) -> Self::CoordType<'_> {
42        &self.end
43    }
44}
45
46#[cfg(feature = "geo-types")]
47impl<'a, T: CoordNum> LineTrait for &'a Line<T> {
48    type CoordType<'b>
49        = &'a Coord<Self::T>
50    where
51        Self: 'b;
52
53    fn start(&self) -> Self::CoordType<'_> {
54        &self.start
55    }
56
57    fn end(&self) -> Self::CoordType<'_> {
58        &self.end
59    }
60}
61
62/// An empty struct that implements [LineTrait].
63///
64/// This can be used as the `LineType` of the `GeometryTrait` by implementations that don't
65/// have a Line concept
66pub struct UnimplementedLine<T>(PhantomData<T>);
67
68impl<T> LineTrait for UnimplementedLine<T> {
69    type CoordType<'a>
70        = UnimplementedCoord<Self::T>
71    where
72        Self: 'a;
73
74    fn start(&self) -> Self::CoordType<'_> {
75        unimplemented!()
76    }
77
78    fn end(&self) -> Self::CoordType<'_> {
79        unimplemented!()
80    }
81}