use std::io;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("ibverbs error: {0}")]
Verbs(#[from] io::Error),
#[error("no RDMA devices found")]
NoDevices,
#[error("device not found: {0}")]
DeviceNotFound(String),
#[error("invalid argument: {0}")]
InvalidArg(String),
#[error("work completion error: {status} (vendor_err={vendor_err:#x})")]
WorkCompletion {
status: u32,
vendor_err: u32,
},
#[error("operation would block")]
WouldBlock,
}
pub(crate) fn from_ret(ret: i32) -> Result<()> {
if ret == 0 {
Ok(())
} else {
Err(Error::Verbs(io::Error::from_raw_os_error(-ret)))
}
}
pub(crate) fn from_ret_errno(ret: i32) -> Result<()> {
if ret == 0 {
Ok(())
} else {
Err(Error::Verbs(io::Error::last_os_error()))
}
}
pub(crate) fn from_ptr<T>(ptr: *mut T) -> Result<*mut T> {
if ptr.is_null() {
Err(Error::Verbs(io::Error::last_os_error()))
} else {
Ok(ptr)
}
}