use std::fmt;
use std::io;
use std::result;
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Io(io::Error),
ZeroSizedMapping,
SizeExceedsSystemLimit,
HugePageAllocationFailed,
NumaAllocationFailed,
ProtectionError,
AlignmentError,
InvalidArgument(String),
PlatformError(i32),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(err) => write!(f, "I/O error: {}", err),
Error::ZeroSizedMapping => write!(f, "Memory map size cannot be zero"),
Error::SizeExceedsSystemLimit => write!(f, "Requested memory map size exceeds system limits"),
Error::HugePageAllocationFailed => write!(f, "Failed to allocate huge pages"),
Error::NumaAllocationFailed => write!(f, "NUMA memory allocation failed"),
Error::ProtectionError => write!(f, "Memory protection error"),
Error::AlignmentError => write!(f, "Memory alignment error"),
Error::InvalidArgument(msg) => write!(f, "Invalid argument: {}", msg),
Error::PlatformError(code) => write!(f, "Platform-specific error code: {}", code),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(err) => Some(err),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::Io(err)
}
}