bidivec/bidiiter/immutable/
border.rs

1use crate::bidiiter::borderstate::IterBorderState;
2use crate::BidiRect;
3use crate::BidiRectSigned;
4use crate::BidiView;
5use std::iter::Iterator;
6
7/// An iterator type returning items in a rectangular region.
8pub struct OnBorder<'v, T: 'v, V: BidiView<Output = T>> {
9    pub(super) view: &'v V,
10    pub(super) rect: BidiRect,
11    pub(super) border: BidiRectSigned,
12    pub(super) state: IterBorderState,
13}
14
15impl<'v, T: 'v, V: BidiView<Output = T>> OnBorder<'v, T, V> {
16    /// Returns an iterator which yields the items with their original
17    /// coordinates. Note that all the coordinates are relative to the
18    /// [`BidiView`] (or other data structure) the iterator was created
19    /// from.
20    pub fn with_coords(self) -> super::super::immutable_xy::border::OnBorder<'v, T, V> {
21        self.state.assert_not_started("with_coords()");
22        super::super::immutable_xy::border::OnBorder {
23            view: self.view,
24            rect: self.rect,
25            border: self.border,
26            state: IterBorderState::NotStarted,
27        }
28    }
29}
30
31impl<'v, T: 'v, V: BidiView<Output = T>> Iterator for OnBorder<'v, T, V> {
32    type Item = &'v T;
33
34    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
35        self.state.advance(&self.rect, &self.border);
36
37        if let IterBorderState::Iterating(x, y) = self.state {
38            self.view.get_signed(x, y)
39        } else {
40            None
41        }
42    }
43}