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
73
74
75
76
77
78
79
80
81
82
pub struct Views<V> {
views: Vec<V>,
}
impl<V> Drop for Views<V> {
fn drop(&mut self) {
self.clear();
}
}
impl<V> Views<V> {
pub fn new(initial: V) -> Self {
let mut new_instance = Self {
views: Vec::with_capacity(3),
};
new_instance.push(initial);
new_instance
}
pub fn push(&mut self, view: V) {
self.views.push(view);
}
pub fn pop(&mut self) {
self.views.pop();
}
pub fn clear(&mut self) {
while !self.is_empty() {
self.pop();
}
}
pub fn current(&self) -> Option<&V> {
self.views.last()
}
pub fn current_mut(&mut self) -> Option<&mut V> {
self.views.last_mut()
}
pub fn is_empty(&self) -> bool {
self.views.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::Views;
#[derive(Eq, PartialEq, Debug)]
enum TestViews {
View1,
View2,
}
#[test]
fn test_new() {
let views = Views::new(TestViews::View1);
assert!(!views.is_empty());
}
#[test]
fn test_push() {
let mut views = Views::new(TestViews::View1);
views.push(TestViews::View2);
assert_eq!(Some(&TestViews::View2), views.current());
}
#[test]
fn test_pop() {
let mut views = Views::new(TestViews::View1);
views.pop();
assert!(views.is_empty());
}
}