use {super::errno, core::ffi::c_int};
#[derive(Debug, Default)]
pub struct SyscallInvoker(Option<c_int>);
impl SyscallInvoker {
pub fn invoke<T, F>(&mut self, f: F) -> Result<T, c_int>
where
F: FnOnce() -> Result<T, ()>,
{
if let Some(errno) = self.0.take() {
Err(errno)
} else {
f().map_err(|()| errno())
}
}
pub fn invoke_standard<T, F>(&mut self, f: F) -> Result<T, c_int>
where
F: FnOnce() -> T,
T: From<i8> + core::cmp::PartialEq,
{
self.invoke(|| {
let rv = f();
if rv == T::from(-1) {
return Err(());
}
Ok(rv)
})
}
#[cfg(feature = "testing")]
pub fn fail_one_syscall_with(&mut self, errno: c_int) {
self.0 = Some(errno);
}
}