refact: Add Context::check_one_call(f)
This makes it easier to stick to a regular pattern and not forget to check the context afterwards. (cherry picked from commit 966ef3769a23075f5d660a48cf5a7307df925dbf)
This commit is contained in:
parent
c775792b63
commit
61efb9b79f
2 changed files with 110 additions and 93 deletions
|
|
@ -34,6 +34,31 @@ impl Context {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn check_err_and_clear(&self) -> Result<()> {
|
||||
let r = self.check_err();
|
||||
if r.is_err() {
|
||||
unsafe {
|
||||
// TODO (https://github.com/NixOS/nix/pull/10910): raw::err_clear
|
||||
raw::set_err_msg(
|
||||
self.inner.as_ptr(),
|
||||
raw::NIX_OK.try_into().unwrap(),
|
||||
b"\0".as_ptr() as *const i8,
|
||||
);
|
||||
}
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
/// Run the function, and check the error, then reset the error.
|
||||
/// Make at most one call to a Nix function in `f`.
|
||||
/// Do not use if the context isn't fresh or cleared (e.g. with `check_err_and_clear`).
|
||||
pub fn check_one_call<T, F: FnOnce(*mut raw::c_context) -> T>(&self, f: F) -> Result<T> {
|
||||
let t = f(self.ptr());
|
||||
self.check_err_and_clear()?;
|
||||
Ok(t)
|
||||
}
|
||||
|
||||
/// NIX_ERR_KEY is returned when e.g. an attribute is missing. Return true if the error is of this type.
|
||||
pub fn is_key_error(&self) -> bool {
|
||||
unsafe { raw::err_code(self.inner.as_ptr()) == raw::NIX_ERR_KEY }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue