From 226639939f9624a1bc81fdd8a8fc198af10734df Mon Sep 17 00:00:00 2001 From: Taeer Bar-Yam Date: Sat, 15 Jun 2024 12:09:11 +0200 Subject: [PATCH] feat: nix_util::context::check_call!(func[...]) > I couldn't figure out a way to use () for the function call, but I didn't try that hard. I (Robert) have also given it a shot, briefly, unsuccessfully. While I was critical of over-engineering this, it turns out that when we start to use `mut`, a macro is much more practical, because it doesn't create a new scope where we need a copy of a mutable reference, which of course is not allowed. (cherry picked from commit a2acc93d13991da4b14a99065acd589477334d07) --- rust/nix-util/src/context.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rust/nix-util/src/context.rs b/rust/nix-util/src/context.rs index 657e85a..75c4b1c 100644 --- a/rust/nix-util/src/context.rs +++ b/rust/nix-util/src/context.rs @@ -96,6 +96,24 @@ impl Drop for Context { } } +#[macro_export] +macro_rules! check_call { + ($f:path[$ctx:expr $(, $arg:expr)*]) => { + { + let ret = $f($ctx.ptr() $(, $arg)*); + match $ctx.check_err() { + Ok(_) => Ok(ret), + Err(e) => { + $ctx.clear(); + Err(e) + } + } + } + } +} + +pub use check_call; + #[cfg(test)] mod tests { use super::*;