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)
This commit is contained in:
Taeer Bar-Yam 2024-06-15 12:09:11 +02:00 committed by Robert Hensing
parent 2f3a5fb039
commit 226639939f

View file

@ -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::*;