e2rcore/implement/util/
loadstack.rs

1#[derive(Clone, Debug)]
2pub struct LoadStack < T > {
3    _buf: Vec< T >,
4}
5
6impl < T > Default for LoadStack < T > {
7    fn default() -> Self {
8        Self {
9            _buf: vec![],
10        }
11    }
12}
13
14impl < T > LoadStack < T > {
15
16    pub fn load( & mut self, t: T ) -> Result< (), & 'static str > {
17        self._buf.push( t );
18        Ok( () )
19    }
20
21    pub fn apply< F >( & mut self, mut f: F ) -> Result< (), & 'static str >
22        where F: FnMut( & T ) -> bool
23    {
24        let mut index = 0;
25
26        {
27            for (k,v) in self._buf.iter().rev().enumerate() {
28                index = k;
29                if !f( v ) { //stop when the function first returns false
30                    break;
31                }
32            }
33        }
34        let temp : Vec< T > = self._buf.drain(..index).collect(); //discard traversed elements
35        self._buf = temp;
36
37        Ok( () )
38    }
39}
40