iced_views/
lib.rs

1pub struct Views<V> {
2    views: Vec<V>,
3}
4
5impl<V> Drop for Views<V> {
6    fn drop(&mut self) {
7        self.clear();
8    }
9}
10
11impl<V> Views<V> {
12    pub fn new(initial: V) -> Self {
13        let mut new_instance = Self {
14            views: Vec::with_capacity(3),
15        };
16
17        new_instance.push(initial);
18        new_instance
19    }
20
21    pub fn push(&mut self, view: V) {
22        self.views.push(view);
23    }
24
25    pub fn pop(&mut self) {
26        self.views.pop();
27    }
28
29    pub fn clear(&mut self) {
30        while !self.is_empty() {
31            self.pop();
32        }
33    }
34
35    pub fn current(&self) -> Option<&V> {
36        self.views.last()
37    }
38
39    pub fn current_mut(&mut self) -> Option<&mut V> {
40        self.views.last_mut()
41    }
42
43    pub fn is_empty(&self) -> bool {
44        self.views.is_empty()
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::Views;
51
52    #[derive(Eq, PartialEq, Debug)]
53    enum TestViews {
54        View1,
55        View2,
56    }
57
58    #[test]
59    fn test_new() {
60        let views = Views::new(TestViews::View1);
61
62        assert!(!views.is_empty());
63    }
64
65    #[test]
66    fn test_push() {
67        let mut views = Views::new(TestViews::View1);
68
69        views.push(TestViews::View2);
70
71        assert_eq!(Some(&TestViews::View2), views.current());
72    }
73
74    #[test]
75    fn test_pop() {
76        let mut views = Views::new(TestViews::View1);
77
78        views.pop();
79
80        assert!(views.is_empty());
81    }
82}