use std::fmt;
use std::io;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Setup(io::Error),
SubmissionQueueFull,
CompletionQueueEmpty,
InvalidOperation(String),
NotSupported(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "I/O error: {}", e),
Error::Setup(e) => write!(f, "io_uring setup failed: {}", e),
Error::SubmissionQueueFull => write!(f, "submission queue is full"),
Error::CompletionQueueEmpty => write!(f, "no completion queue entries available"),
Error::InvalidOperation(msg) => write!(f, "invalid operation: {}", msg),
Error::NotSupported(msg) => write!(f, "feature not supported: {}", msg),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) | Error::Setup(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}
pub(crate) fn from_ret_code(ret: i32) -> io::Error {
io::Error::from_raw_os_error(-ret)
}
pub(crate) fn check_ret(ret: i32) -> io::Result<i32> {
if ret < 0 {
Err(from_ret_code(ret))
} else {
Ok(ret)
}
}