mirror of
https://github.com/koverstreet/bcachefs-tools.git
synced 2025-12-08 00:00:12 +03:00
Add ret_to_result to convert int into Result<c_int, bch_errcode>
This commit is contained in:
parent
454a6b9ade
commit
235c11e325
@ -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> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user