refactor: Remove check_one_call

(cherry picked from commit 065f880e52c6d6cb44e4b857272176ebe2464eea)
This commit is contained in:
Robert Hensing 2024-06-15 14:51:26 +02:00
parent bf6dbd3f1e
commit 35803f4a30
5 changed files with 48 additions and 70 deletions

View file

@ -61,15 +61,6 @@ impl Context {
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>(&mut self, f: F) -> Result<T> {
let t = f(self.ptr());
self.check_err_and_clear()?;
Ok(t)
}
pub fn check_one_call_or_key_none<T, F: FnOnce(*mut raw::c_context) -> T>(
&mut self,
f: F,

View file

@ -2,7 +2,7 @@ use anyhow::Result;
use nix_c_raw as raw;
use crate::{
context, result_string_init,
check_call, context, result_string_init,
string_return::{callback_get_result_string, callback_get_result_string_data},
};
@ -10,23 +10,19 @@ pub fn set(key: &str, value: &str) -> Result<()> {
let mut ctx = context::Context::new();
let key = std::ffi::CString::new(key)?;
let value = std::ffi::CString::new(value)?;
ctx.check_one_call(|ctx_ptr| unsafe {
raw::setting_set(ctx_ptr, key.as_ptr(), value.as_ptr());
})
unsafe {
check_call!(raw::setting_set[&mut ctx, key.as_ptr(), value.as_ptr()])?;
}
Ok(())
}
pub fn get(key: &str) -> Result<String> {
let mut ctx = context::Context::new();
let key = std::ffi::CString::new(key)?;
let mut r: Result<String> = result_string_init!();
ctx.check_one_call(|ctx_ptr| unsafe {
raw::setting_get(
ctx_ptr,
key.as_ptr(),
Some(callback_get_result_string),
callback_get_result_string_data(&mut r),
)
})?;
unsafe {
check_call!(raw::setting_get[&mut ctx, key.as_ptr(), Some(callback_get_result_string), callback_get_result_string_data(&mut r)])?;
}
r
}