Add ret_to_result to convert int into Result<c_int, bch_errcode>

This commit is contained in:
beviu 2025-09-20 22:15:01 +02:00
parent 454a6b9ade
commit 235c11e325
No known key found for this signature in database
GPG Key ID: C6AAE70FD32E0112

View File

@ -1,5 +1,5 @@
use crate::bcachefs;
use std::ffi::CStr;
use std::ffi::{c_int, CStr};
use std::fmt;
pub use crate::c::bch_errcode;
@ -11,6 +11,16 @@ impl fmt::Display for bch_errcode {
}
}
pub fn ret_to_result(ret: c_int) -> Result<c_int, bch_errcode> {
let max_err: c_int = -4096;
if ret < 0 && ret > max_err {
let err: bch_errcode = unsafe { std::mem::transmute(-ret) };
Err(err)
} else {
Ok(ret)
}
}
/* Can we make a function generic over ptr constness? */
pub fn errptr_to_result<T>(p: *mut T) -> Result<*mut T, bch_errcode> {