check_call!() macro to use () not []

(cherry picked from commit 66d255af0a5d331782dc24c89bb45d3434f3c109)
This commit is contained in:
Taeer Bar-Yam 2024-07-02 16:35:32 -04:00 committed by Robert Hensing
parent da26721bea
commit cc2e640e7f
5 changed files with 64 additions and 39 deletions

View file

@ -16,7 +16,7 @@ lazy_static! {
static ref INIT: Result<()> = {
unsafe {
raw::GC_allow_register_threads();
check_call!(raw::libexpr_init[&mut Context::new()])?;
check_call!(raw::libexpr_init(&mut Context::new()))?;
Ok(())
}
};
@ -67,7 +67,11 @@ impl EvalState {
init()?;
let eval_state = unsafe {
check_call!(raw::state_create[&mut context, lookup_path.as_mut_ptr(), store.raw_ptr()])
check_call!(raw::state_create(
&mut context,
lookup_path.as_mut_ptr(),
store.raw_ptr()
))
}?;
Ok(EvalState {
eval_state: NonNull::new(eval_state).unwrap_or_else(|| {
@ -109,30 +113,29 @@ impl EvalState {
CString::new(path).with_context(|| "eval_from_string: path contains null byte")?;
unsafe {
let value = self.new_value_uninitialized()?;
check_call!(raw::expr_eval_from_string[
check_call!(raw::expr_eval_from_string(
&mut self.context,
self.eval_state.as_ptr(),
expr_ptr.as_ptr(),
path_ptr.as_ptr(),
value.raw_ptr()
])?;
))?;
Ok(value)
}
}
/// Try turn any Value into a Value that isn't a Thunk.
pub fn force(&mut self, v: &Value) -> Result<()> {
unsafe {
check_call!(raw::value_force[&mut self.context, self.eval_state.as_ptr(), v.raw_ptr()])
check_call!(raw::value_force(
&mut self.context,
self.eval_state.as_ptr(),
v.raw_ptr()
))
}?;
Ok(())
}
pub fn value_type_unforced(&mut self, value: &Value) -> Option<ValueType> {
let r = unsafe {
check_call!(raw::get_type[
&mut self.context,
value.raw_ptr()
])
};
let r = unsafe { check_call!(raw::get_type(&mut self.context, value.raw_ptr())) };
// .unwrap(): no reason for this to fail, as it does not evaluate
ValueType::from_raw(r.unwrap())
}
@ -155,7 +158,7 @@ impl EvalState {
if t != ValueType::Int {
bail!("expected an int, but got a {:?}", t);
}
unsafe { check_call!(raw::get_int[&mut self.context, v.raw_ptr()]) }
unsafe { check_call!(raw::get_int(&mut self.context, v.raw_ptr())) }
}
/// Evaluate, and require that the value is an attrset.
/// Returns a list of the keys in the attrset.
@ -164,16 +167,16 @@ impl EvalState {
if t != ValueType::AttrSet {
bail!("expected an attrset, but got a {:?}", t);
}
let n = unsafe { check_call!(raw::get_attrs_size[&mut self.context, v.raw_ptr()]) }?;
let n = unsafe { check_call!(raw::get_attrs_size(&mut self.context, v.raw_ptr())) }?;
let mut attrs = Vec::with_capacity(n as usize);
for i in 0..n {
let cstr_ptr: *const i8 = unsafe {
check_call!(raw::get_attr_name_byidx[
check_call!(raw::get_attr_name_byidx(
&mut self.context,
v.raw_ptr(),
self.eval_state.as_ptr(),
i as c_uint
])
))
}?;
let cstr = unsafe { std::ffi::CStr::from_ptr(cstr_ptr) };
let s = cstr
@ -193,12 +196,12 @@ impl EvalState {
let attr_name = CString::new(attr_name)
.with_context(|| "require_attrs_select: attrName contains null byte")?;
let v2 = unsafe {
check_call!(raw::get_attr_byname[
check_call!(raw::get_attr_byname(
&mut self.context,
v.raw_ptr(),
self.eval_state.as_ptr(),
attr_name.as_ptr()
])
))
}?;
Ok(Value::new(v2))
}
@ -238,7 +241,11 @@ impl EvalState {
let s = CString::new(s).with_context(|| "new_value_str: contains null byte")?;
let v = unsafe {
let value = self.new_value_uninitialized()?;
check_call!(raw::init_string[&mut self.context, value.raw_ptr(), s.as_ptr()])?;
check_call!(raw::init_string(
&mut self.context,
value.raw_ptr(),
s.as_ptr()
))?;
value
};
Ok(v)
@ -247,7 +254,7 @@ impl EvalState {
pub fn new_value_int(&mut self, i: Int) -> Result<Value> {
let v = unsafe {
let value = self.new_value_uninitialized()?;
check_call!(raw::init_int[&mut self.context, value.raw_ptr(), i])?;
check_call!(raw::init_int(&mut self.context, value.raw_ptr(), i))?;
value
};
Ok(v)
@ -257,12 +264,12 @@ impl EvalState {
fn get_string(&mut self, value: &Value) -> Result<String> {
let mut r = result_string_init!();
unsafe {
check_call!(raw::get_string[
check_call!(raw::get_string(
&mut self.context,
value.raw_ptr(),
Some(callback_get_result_string),
callback_get_result_string_data(&mut r)
])?;
))?;
};
r
}
@ -285,12 +292,12 @@ impl EvalState {
}
let rs = unsafe {
check_call!(raw::string_realise[
check_call!(raw::string_realise(
&mut self.context,
self.eval_state.as_ptr(),
value.raw_ptr(),
is_import_from_derivation
])
))
}?;
let s = unsafe {
@ -325,13 +332,13 @@ impl EvalState {
pub fn call(&mut self, f: Value, a: Value) -> Result<Value> {
let value = self.new_value_uninitialized()?;
unsafe {
check_call!(raw::value_call[
check_call!(raw::value_call(
&mut self.context,
self.eval_state.as_ptr(),
f.raw_ptr(),
a.raw_ptr(),
value.raw_ptr()
])
))
}?;
Ok(value)
}
@ -342,19 +349,23 @@ impl EvalState {
pub fn new_value_apply(&mut self, f: &Value, a: &Value) -> Result<Value> {
let value = self.new_value_uninitialized()?;
unsafe {
check_call!(raw::init_apply[
check_call!(raw::init_apply(
&mut self.context,
value.raw_ptr(),
f.raw_ptr(),
a.raw_ptr()
])
))
}?;
Ok(value)
}
fn new_value_uninitialized(&mut self) -> Result<Value> {
let value =
unsafe { check_call!(raw::alloc_value[&mut self.context, self.eval_state.as_ptr()]) }?;
let value = unsafe {
check_call!(raw::alloc_value(
&mut self.context,
self.eval_state.as_ptr()
))
}?;
Ok(Value::new(value))
}
}

View file

@ -77,7 +77,7 @@ impl Clone for Value {
// this is very unlikely to error, and it is not recoverable
// Maybe try without, and try again with context to report details?
unsafe {
check_call!(raw::gc_incref[&mut Context::new(), self.inner.as_ptr()]).unwrap();
check_call!(raw::gc_incref(&mut Context::new(), self.inner.as_ptr())).unwrap();
}
// can't return an error here, but we don't want to ignore the error either as it means we could use-after-free
Value { inner: self.inner }

View file

@ -11,7 +11,7 @@ use std::ptr::NonNull;
/* TODO make Nix itself thread safe */
lazy_static! {
static ref INIT: Result<()> = unsafe {
check_call!(raw::libstore_init[&mut Context::new()])?;
check_call!(raw::libstore_init(&mut Context::new()))?;
Ok(())
};
}
@ -74,7 +74,11 @@ impl Store {
.collect();
let store = unsafe {
check_call!(raw::store_open[&mut context, uri_ptr.as_ptr(), params.as_mut_ptr()])
check_call!(raw::store_open(
&mut context,
uri_ptr.as_ptr(),
params.as_mut_ptr()
))
}?;
if store.is_null() {
panic!("nix_c_store_open returned a null pointer without an error");
@ -95,7 +99,12 @@ impl Store {
pub fn get_uri(&mut self) -> Result<String> {
let mut r = result_string_init!();
unsafe {
check_call!(raw::store_get_uri[&mut self.context, self.inner.ptr(), Some(callback_get_result_string), callback_get_result_string_data(&mut r)])
check_call!(raw::store_get_uri(
&mut self.context,
self.inner.ptr(),
Some(callback_get_result_string),
callback_get_result_string_data(&mut r)
))
}?;
r
}

View file

@ -85,10 +85,10 @@ impl Drop for Context {
#[macro_export]
macro_rules! check_call {
($f:path[$ctx:expr $(, $arg:expr)*]) => {
($($f:ident)::+($ctx:expr $(, $arg:expr)*)) => {
{
let ctx : &mut $crate::context::Context = $ctx;
let ret = $f(ctx.ptr() $(, $arg)*);
let ret = $($f)::*(ctx.ptr() $(, $arg)*);
match ctx.check_err() {
Ok(_) => Ok(ret),
Err(e) => {
@ -148,7 +148,7 @@ mod tests {
#[test]
fn check_call_dynamic_context() {
let r = check_call!(set_dummy_err[&mut Context::new()]);
let r = check_call!(set_dummy_err(&mut Context::new()));
assert!(r.is_err());
assert_eq!(r.unwrap_err().to_string(), "dummy error message");
}

View file

@ -11,7 +11,7 @@ pub fn set(key: &str, value: &str) -> Result<()> {
let key = std::ffi::CString::new(key)?;
let value = std::ffi::CString::new(value)?;
unsafe {
check_call!(raw::setting_set[&mut ctx, key.as_ptr(), value.as_ptr()])?;
check_call!(raw::setting_set(&mut ctx, key.as_ptr(), value.as_ptr()))?;
}
Ok(())
}
@ -21,7 +21,12 @@ pub fn get(key: &str) -> Result<String> {
let key = std::ffi::CString::new(key)?;
let mut r: Result<String> = result_string_init!();
unsafe {
check_call!(raw::setting_get[&mut ctx, key.as_ptr(), Some(callback_get_result_string), callback_get_result_string_data(&mut r)])?;
check_call!(raw::setting_get(
&mut ctx,
key.as_ptr(),
Some(callback_get_result_string),
callback_get_result_string_data(&mut r)
))?;
}
r
}
@ -36,7 +41,7 @@ mod tests {
fn setup() {
let mut ctx = context::Context::new();
unsafe {
check_call!(raw::libstore_init[&mut ctx]).unwrap();
check_call!(raw::libstore_init(&mut ctx)).unwrap();
}
}