rs_graph/collections/
stack.rs

1/*
2 * Copyright (c) 2018 Frank Fischer <frank-fischer@shadow-soft.de>
3 *
4 * This program is free software: you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see  <http://www.gnu.org/licenses/>
16 */
17
18pub trait ItemStack<I> {
19    fn is_empty(&self) -> bool {
20        self.len() == 0
21    }
22
23    fn len(&self) -> usize;
24
25    fn clear(&mut self);
26
27    fn push(&mut self, u: I);
28
29    fn pop(&mut self) -> Option<I>;
30
31    fn top(&self) -> Option<&I>;
32
33    fn top_mut(&mut self) -> Option<&mut I>;
34}
35
36impl<'a, I, D> ItemStack<I> for &'a mut D
37where
38    D: ItemStack<I>,
39{
40    fn is_empty(&self) -> bool {
41        (**self).is_empty()
42    }
43
44    fn len(&self) -> usize {
45        (**self).len()
46    }
47
48    fn clear(&mut self) {
49        (**self).clear()
50    }
51
52    fn push(&mut self, u: I) {
53        (**self).push(u)
54    }
55
56    fn pop(&mut self) -> Option<I> {
57        (**self).pop()
58    }
59
60    fn top(&self) -> Option<&I> {
61        (**self).top()
62    }
63
64    fn top_mut(&mut self) -> Option<&mut I> {
65        (**self).top_mut()
66    }
67}
68
69impl<I> ItemStack<I> for Vec<I> {
70    fn is_empty(&self) -> bool {
71        Vec::is_empty(self)
72    }
73
74    fn len(&self) -> usize {
75        Vec::len(self)
76    }
77
78    fn clear(&mut self) {
79        Vec::clear(self)
80    }
81
82    fn push(&mut self, u: I) {
83        Vec::push(self, u)
84    }
85
86    fn pop(&mut self) -> Option<I> {
87        Vec::pop(self)
88    }
89
90    fn top(&self) -> Option<&I> {
91        self.as_slice().last()
92    }
93
94    fn top_mut(&mut self) -> Option<&mut I> {
95        self.as_mut_slice().last_mut()
96    }
97}