-
Notifications
You must be signed in to change notification settings - Fork 110
/
iterator.rs
72 lines (64 loc) · 1.73 KB
/
iterator.rs
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
72
//! Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
trait Iterator<T> {
fn next(&mut self) -> Option<T>;
fn current(&self) -> Option<T>;
fn has_next(&self) -> bool;
fn reset(&mut self);
}
struct Container<T> {
data: Vec<T>,
}
struct ConcreteIterator<'a, T> {
idx: usize,
container: &'a Container<T>,
}
impl<'a, T: Clone> ConcreteIterator<'a, T> {
fn new(container: &'a Container<T>) -> ConcreteIterator<T> {
ConcreteIterator { idx: 0, container }
}
}
impl<'a, T: Clone> Iterator<T> for ConcreteIterator<'a, T> {
fn next(&mut self) -> Option<T> {
let item = self.container.data.get(self.idx).cloned();
self.idx += 1;
item
}
fn current(&self) -> Option<T> {
self.container.data.get(self.idx).cloned()
}
fn has_next(&self) -> bool {
self.container.data.len() > self.idx
}
fn reset(&mut self) {
self.idx = 0;
}
}
impl<T: Clone> Container<T> {
fn new() -> Container<T> {
Container { data: Vec::new() }
}
fn add_item(&mut self, item: T) {
self.data.push(item);
}
fn iter(&self) -> impl Iterator<T> + '_ {
ConcreteIterator::new(self)
}
}
fn main() {
let mut c = Container::new();
c.add_item(1);
c.add_item(2);
c.add_item(3);
let mut iter = c.iter();
let has_next = iter.has_next();
assert_eq!(has_next, true);
let item = iter.next();
println!("item: {:?}", item);
iter.reset();
while iter.has_next() {
let v = iter.next().unwrap();
println!("item: {}", v);
}
let item = iter.next();
assert_eq!(item, None);
}