mirror of
https://github.com/griffi-gh/hUI.git
synced 2024-11-21 14:48:42 -06:00
add state scope function
This commit is contained in:
parent
aa9e0de3e9
commit
92f8975702
|
@ -123,4 +123,20 @@ impl StateRepo {
|
|||
std::mem::swap(&mut self.id_stack, &mut self.standby);
|
||||
ret
|
||||
}
|
||||
|
||||
/// Scope the state repo
|
||||
///
|
||||
/// Anything pushed or popped will be discarded after the closure,
|
||||
/// and the stack will be restored to its previous state
|
||||
pub fn scope<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R {
|
||||
self.standby.clear();
|
||||
self.standby.extend(self.id_stack.iter().copied());
|
||||
let ret = f(self);
|
||||
std::mem::swap(&mut self.id_stack, &mut self.standby);
|
||||
ret
|
||||
//XXX: this is super efficient, but works only for pushes, if anything is popped, it will be lost
|
||||
// let len = self.id_stack.len();
|
||||
// let ret = f(self);
|
||||
// self.id_stack.truncate(len);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue