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
use std::fmt;
#[derive(Clone, Debug)]
pub struct PortSmartIterator {
start: u16,
end: u16,
curr: u16,
next: Option<u16>,
updater: fn(u16) -> u16
}
impl PortSmartIterator {
pub fn new (start: u16, end: u16) -> PortSmartIterator {
PortSmartIterator {
start,
end,
curr: start,
next: Some(start),
updater: if start < end { |x| x + 1 } else { |x| x - 1 }
}
}
}
impl Iterator for PortSmartIterator {
type Item = u16;
fn next (&mut self) -> Option<Self::Item> {
self.curr = self.next?;
let update = self.updater;
self.next = if self.curr == self.end { None } else { Some(update(self.curr)) };
Some(self.curr)
}
}
impl fmt::Display for PortSmartIterator {
fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}-{}", self.start, self.curr, self.end)
}
}