use core::fmt;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
PoolExhausted {
capacity: usize,
allocated: usize,
},
InvalidConfiguration {
message: &'static str,
},
UninitializedPool,
InvalidAlignment {
alignment: usize,
},
MaxCapacityExceeded {
current: usize,
requested: usize,
max: usize,
},
InvalidHandle,
DoubleFree,
AllocationFailed,
Custom {
message: &'static str,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::PoolExhausted {
capacity,
allocated,
} => {
write!(
f,
"Pool exhausted: allocated {}/{} objects. Consider using a growing pool or increasing capacity.",
allocated, capacity
)
}
Error::InvalidConfiguration { message } => {
write!(f, "Invalid pool configuration: {}", message)
}
Error::UninitializedPool => {
write!(f, "Attempted to use an uninitialized pool")
}
Error::InvalidAlignment { alignment } => {
write!(
f,
"Invalid alignment: {}. Alignment must be a power of two.",
alignment
)
}
Error::MaxCapacityExceeded {
current,
requested,
max,
} => {
write!(
f,
"Maximum capacity exceeded: current={}, requested={}, max={}",
current, requested, max
)
}
Error::InvalidHandle => {
write!(f, "Invalid or expired handle")
}
Error::DoubleFree => {
write!(f, "Attempted to free an already freed object (double-free)")
}
Error::AllocationFailed => {
write!(f, "System memory allocation failed")
}
Error::Custom { message } => {
write!(f, "Error: {}", message)
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl Error {
#[inline]
pub fn invalid_config(message: &'static str) -> Self {
Error::InvalidConfiguration { message }
}
#[inline]
pub fn custom(message: &'static str) -> Self {
Error::Custom { message }
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
#[test]
fn error_display() {
let err = Error::PoolExhausted {
capacity: 100,
allocated: 100,
};
assert!(err.to_string().contains("exhausted"));
let err = Error::InvalidConfiguration {
message: "capacity must be positive",
};
assert!(err.to_string().contains("capacity must be positive"));
let err = Error::InvalidAlignment { alignment: 7 };
assert!(err.to_string().contains("power of two"));
}
#[test]
fn error_helpers() {
let err = Error::invalid_config("test");
assert!(matches!(err, Error::InvalidConfiguration { .. }));
let err = Error::custom("custom message");
assert!(matches!(err, Error::Custom { .. }));
}
}