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
use crate::{Line, Point}; use ::std::iter::Iterator; pub struct LineIterator<'a> { points: &'a [f32], } impl<'a> LineIterator<'a> { pub fn new(points: &'a [f32]) -> Self { assert!( (points.len() % 4) == 0, "length of points given should be divisible by 4, instead divisible by {}", points.len() ); Self { points } } } impl<'a> Iterator for LineIterator<'a> { type Item = Line; fn next(&mut self) -> Option<Self::Item> { if self.points.len() > 0 { let start = Point(self.points[0], self.points[1]); let end = Point(self.points[2], self.points[3]); let line = Line(start, end); self.points = &self.points[4..]; return Some(line); } None } } #[cfg(test)] mod test { use super::*; #[test] fn it_should_return_an_empty_list_if_no_points() { let iterator = LineIterator::new(&[]); let lines: Vec<Line> = iterator.collect(); assert_eq!(lines, []); } #[test] fn it_should_return_the_lines_given() { let iterator = LineIterator::new(&[ 0.0, 1.0, 10.0, 13.0, 20.0, 21.0, 210.0, 213.0, 0.0, 1.0, 10.0, 13.0, 20.0, 21.0, 210.0, 213.0, 0.0, 1.0, 10.0, 13.0, 20.0, 21.0, 210.0, 213.0, ]); let lines: Vec<Line> = iterator.collect(); assert_eq!( lines, [ Line(Point(0.0, 1.0), Point(10.0, 13.0)), Line(Point(20.0, 21.0), Point(210.0, 213.0)), Line(Point(0.0, 1.0), Point(10.0, 13.0)), Line(Point(20.0, 21.0), Point(210.0, 213.0)), Line(Point(0.0, 1.0), Point(10.0, 13.0)), Line(Point(20.0, 21.0), Point(210.0, 213.0)), ] ); } }