From 92f8975702931f4d0fde40feff8c73945a100601 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Fri, 29 Mar 2024 14:27:57 +0100 Subject: [PATCH] add state scope function --- hui/src/state.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/hui/src/state.rs b/hui/src/state.rs index 333c47f..d9598b1 100644 --- a/hui/src/state.rs +++ b/hui/src/state.rs @@ -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(&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); + } }