From 4e2381dc3b274c433e33a89ddf319017129ab575 Mon Sep 17 00:00:00 2001 From: Edward Amsden Date: Tue, 28 Nov 2023 10:57:33 -0600 Subject: [PATCH] 2stackz: add function to wrap a closure in frame push and pop --- rust/ares/src/mem.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rust/ares/src/mem.rs b/rust/ares/src/mem.rs index 90cb32e..f53b63b 100644 --- a/rust/ares/src/mem.rs +++ b/rust/ares/src/mem.rs @@ -656,6 +656,21 @@ impl NockStack { self.pc = false; } + /** Run a closure inside a frame, popping regardless of the value returned by the closure. + * This is useful for writing fallible computations with the `?` operator. + * + * Note that results allocated on the stack *must* be `preserve()`d by the closure. + */ + pub unsafe fn with_frame(&mut self, num_locals: usize, f: F) -> O + where + F: FnOnce() -> O, + { + self.frame_push(num_locals); + let ret = f(); + self.frame_pop(); + ret + } + /** Lightweight stack. * The lightweight stack is a stack data structure present in each stack * frame, often used for noun traversal. During normal operation (self.pc