ash_alloc/
error.rs

1//! Crate errors.
2
3use std::error::Error;
4
5/// Errors that the allocators can throw.
6#[derive(Debug, Eq, PartialEq)]
7pub enum AllocatorError {
8    /// A `TryFromIntError`.
9    TryFromIntError(std::num::TryFromIntError),
10    /// General out of memory error.
11    OutOfMemory,
12    /// Failed to map the memory.
13    FailedToMap,
14    /// No free slots ara available.
15    NotSlotsAvailable,
16    /// No compatible memory type was found.
17    NoCompatibleMemoryTypeFound,
18    /// Alignment is not a power of 2.
19    InvalidAlignment,
20    /// Can't find referenced chunk in chunk list.
21    CantFindChunk,
22    /// Can't find referenced block in block list.
23    CantFindBlock,
24    /// An allocator implementation error.
25    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}