add state scope function

This commit is contained in:
griffi-gh 2024-03-29 14:27:57 +01:00
parent aa9e0de3e9
commit 92f8975702

View file

@ -123,4 +123,20 @@ impl StateRepo {
std::mem::swap(&mut self.id_stack, &mut self.standby); std::mem::swap(&mut self.id_stack, &mut self.standby);
ret 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);
}
} }