fix: Require mutable Context, as it should be

This spreads out transitively to many places and requires that
we use `check_call!` instead of `check_one_call` in a number of
places.

(cherry picked from commit 6bc31dcf206518a7be7f0ac6e773d5dfe25531ea)
This commit is contained in:
Robert Hensing 2024-06-15 12:40:45 +02:00
parent 226639939f
commit a6dbf17778
5 changed files with 143 additions and 129 deletions

View file

@ -47,7 +47,7 @@ impl Context {
Ok(())
}
fn clear(&self) {
pub fn clear(&mut self) {
unsafe {
raw::set_err_msg(
self.inner.as_ptr(),
@ -57,7 +57,7 @@ impl Context {
}
}
pub fn check_err_and_clear(&self) -> Result<()> {
pub fn check_err_and_clear(&mut self) -> Result<()> {
let r = self.check_err();
if r.is_err() {
self.clear();
@ -68,14 +68,14 @@ impl Context {
/// 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> {
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>(
&self,
&mut self,
f: F,
) -> Result<Option<T>> {
let t = f(self.ptr());

View file

@ -7,7 +7,7 @@ use crate::{
};
pub fn set(key: &str, value: &str) -> Result<()> {
let ctx = context::Context::new();
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 {
@ -16,7 +16,7 @@ pub fn set(key: &str, value: &str) -> Result<()> {
}
pub fn get(key: &str) -> Result<String> {
let ctx = context::Context::new();
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 {
@ -32,15 +32,16 @@ pub fn get(key: &str) -> Result<String> {
#[cfg(test)]
mod tests {
use crate::check_call;
use super::*;
#[ctor::ctor]
fn setup() {
let ctx = context::Context::new();
ctx.check_one_call(|ctx_ptr| unsafe {
nix_c_raw::libstore_init(ctx_ptr);
})
.unwrap();
let mut ctx = context::Context::new();
unsafe {
check_call!(raw::libstore_init[ctx]).unwrap();
}
}
#[test]