1use std::error::Error;
4
5#[derive(Debug, Eq, PartialEq)]
7pub enum AllocatorError {
8 TryFromIntError(std::num::TryFromIntError),
10 OutOfMemory,
12 FailedToMap,
14 NotSlotsAvailable,
16 NoCompatibleMemoryTypeFound,
18 InvalidAlignment,
20 CantFindChunk,
22 CantFindBlock,
24 Internal(String),
26}
27
28impl std::fmt::Display for AllocatorError {
29 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30 match self {
31 AllocatorError::TryFromIntError(err) => {
32 write!(f, "{:?}", err.source())
33 }
34 AllocatorError::OutOfMemory => {
35 write!(f, "out of memory")
36 }
37 AllocatorError::FailedToMap => {
38 write!(f, "failed to map memory")
39 }
40 AllocatorError::NotSlotsAvailable => {
41 write!(f, "no free slots available")
42 }
43 AllocatorError::NoCompatibleMemoryTypeFound => {
44 write!(f, "no compatible memory type available")
45 }
46 AllocatorError::InvalidAlignment => {
47 write!(f, "alignment is not a power of 2")
48 }
49 AllocatorError::Internal(message) => {
50 write!(f, "{}", message)
51 }
52 AllocatorError::CantFindChunk => {
53 write!(f, "can't find chunk in chunk list")
54 }
55 AllocatorError::CantFindBlock => {
56 write!(f, "can't find block in block list")
57 }
58 }
59 }
60}
61
62impl From<std::num::TryFromIntError> for AllocatorError {
63 fn from(err: std::num::TryFromIntError) -> AllocatorError {
64 AllocatorError::TryFromIntError(err)
65 }
66}
67
68impl std::error::Error for AllocatorError {
69 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
70 match *self {
71 AllocatorError::TryFromIntError(ref e) => Some(e),
72 _ => None,
73 }
74 }
75}