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
//! Path types and tools.

use crate::P2;
use lyon_geom::LineSegment;
use lyon_path::PathEvent;

/// An adapter for path iterators that implements `Path` and closes the path at the end.
#[derive(Clone)]
pub struct ClosedPath<P> {
    src: P,
    done: bool,
}

impl<P> From<P> for ClosedPath<P>
where
    P: Iterator<Item = PathEvent> + Clone,
{
    fn from(src: P) -> Self { Self { src, done: false } }
}

impl<P> Iterator for ClosedPath<P>
where
    P: Iterator<Item = PathEvent>,
{
    type Item = PathEvent;
    fn next(&mut self) -> Option<Self::Item> {
        match self.src.next() {
            Some(e) => Some(e),
            None if !self.done => {
                self.done = true;
                Some(PathEvent::Close(LineSegment {
                    from: P2::new(0., 0.),
                    to: P2::new(0., 0.),
                }))
            }
            _ => None,
        }
    }
}

/// An adapter for iterators over points that implements `Path`.
#[derive(Debug, Copy, Clone)]
pub struct FlatIterPath<I> {
    src: I,
    last: Option<P2>,
}

impl<I> From<I> for FlatIterPath<I>
where
    I: Iterator<Item = P2>,
{
    fn from(src: I) -> Self { Self { src, last: None } }
}

impl<I> Iterator for FlatIterPath<I>
where
    I: Iterator<Item = P2>,
{
    type Item = PathEvent;
    fn next(&mut self) -> Option<Self::Item> {
        if let Some(last) = self.last.take() {
            let p = self.src.next()?;
            self.last = Some(p);
            Some(PathEvent::Line(LineSegment { from: last, to: p }))
        } else {
            let p = self.src.next()?;
            self.last = Some(p);
            Some(PathEvent::MoveTo(p))
        }
    }
}