Skip to content

Commit

Permalink
Index<usize> for LineString to get the coordinate at that position
Browse files Browse the repository at this point in the history
  • Loading branch information
Rory McCann committed Feb 13, 2019
1 parent 96c7846 commit c4f27e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion geo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ mod test {
let _: MultiPoint<_> = vec![(0., 0.), (1., 2.)].into();
let _: MultiPoint<_> = vec![(0., 0.), (1., 2.)].into_iter().collect();

let _: LineString<_> = vec![(0., 0.), (1., 2.)].into();
let mut l1: LineString<_> = vec![(0., 0.), (1., 2.)].into();
assert_eq!(l1[1], Coordinate{ x: 1., y: 2. }); // index into linestring
let _: LineString<_> = vec![(0., 0.), (1., 2.)].into_iter().collect();

// index mutably into a linestring
l1[0] = Coordinate{ x: 1., y: 1. };
assert_eq!(l1, vec![(1., 1.), (1., 2.)].into());
}

#[test]
Expand Down
16 changes: 16 additions & 0 deletions geo-types/src/line_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::iter::FromIterator;
use std::ops::{Index, IndexMut};
use {Coordinate, CoordinateType, Line, Point, Triangle};

/// An ordered collection of two or more [`Coordinate`s](struct.Coordinate.html), representing a
Expand Down Expand Up @@ -174,6 +175,21 @@ impl<T: CoordinateType> IntoIterator for LineString<T> {
}
}

impl<T: CoordinateType> Index<usize> for LineString<T> {
type Output = Coordinate<T>;

fn index(&self, index: usize) -> &Coordinate<T> {
self.0.index(index)
}
}

impl<T: CoordinateType> IndexMut<usize> for LineString<T> {
fn index_mut(&mut self, index: usize) -> &mut Coordinate<T> {
self.0.index_mut(index)
}
}


#[cfg(feature = "rstar")]
impl<T> ::rstar::RTreeObject for LineString<T>
where
Expand Down

0 comments on commit c4f27e9

Please sign in to comment.