From 16f82b02a07110ae3f4133758d3a7e20ca2401ea Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 29 Sep 2024 04:18:47 +0000 Subject: [PATCH] add util to restore state on scope exit Signed-off-by: Jason Volk --- src/core/utils/defer.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/utils/defer.rs b/src/core/utils/defer.rs index 08477b6f..29199700 100644 --- a/src/core/utils/defer.rs +++ b/src/core/utils/defer.rs @@ -15,8 +15,14 @@ macro_rules! defer { }; ($body:expr) => { - $crate::defer! {{ - $body - }} + $crate::defer! {{ $body }} + }; +} + +#[macro_export] +macro_rules! scope_restore { + ($val:ident, $ours:expr) => { + let theirs = $crate::utils::exchange($val, $ours); + $crate::defer! {{ *$val = theirs; }}; }; }