e2rcore/implement/util/
loadsingle.rs

1#[derive(Clone, Debug)]
2pub struct LoadSingle < T > {
3    _buf: Option< T >,
4    ///if true, no-load if there exists data
5    pub _is_blocking: bool,
6}
7
8impl < T > Default for LoadSingle < T > {
9    fn default() -> Self {
10        Self {
11            _buf: None,
12            _is_blocking: false,
13        }
14    }
15}
16
17impl < T > LoadSingle < T > {
18
19    pub fn load( & mut self, t: T ) -> Result< (), & 'static str > {
20        if self._is_blocking {
21            if self._buf.is_none() {
22                self._buf = Some( t );
23            }
24        } else {
25            self._buf = Some( t );
26        }
27        
28        Ok( () )
29    }
30
31    pub fn apply< F >( & mut self, mut f: F ) -> Result< (), & 'static str >
32        where F: FnMut( & T ) -> bool
33    {
34
35        let consumed = match self._buf {
36            None => { false }, //do nothing if no data
37            Some(ref x) => { f( x ); true },
38        };
39
40        if consumed {
41            self._buf = None;
42        }
43
44        Ok( () )
45    }
46}
47