alloc_madvise/
alloc_result.rs1use std::alloc::LayoutError;
4use std::error::Error;
5use std::fmt::{Display, Formatter};
6
7#[derive(Debug, PartialEq)]
8pub enum AllocationError {
9 EmptyAllocation,
11 InvalidAlignment(LayoutError),
13}
14
15impl Error for AllocationError {}
16
17impl From<LayoutError> for AllocationError {
18 fn from(value: LayoutError) -> Self {
19 Self::InvalidAlignment(value)
20 }
21}
22
23impl Display for AllocationError {
24 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25 match self {
26 AllocationError::EmptyAllocation => f.write_str("zero-byte allocation"),
27 AllocationError::InvalidAlignment(e) => write!(f, "invalid memory layout: {e}"),
28 }
29 }
30}
31
32impl From<AllocationError> for AllocResult {
33 fn from(val: AllocationError) -> Self {
34 match val {
35 AllocationError::EmptyAllocation => AllocResult::Empty,
36 AllocationError::InvalidAlignment(_) => AllocResult::InvalidAlignment,
37 }
38 }
39}
40
41#[repr(u32)]
42#[derive(PartialEq, Eq, Copy, Clone, Debug)]
43pub enum AllocResult {
44 Ok = 0,
45 Empty = 1 << 0,
46 InvalidAlignment = 1 << 1,
47}
48
49impl From<u32> for AllocResult {
50 fn from(value: u32) -> Self {
51 match value {
52 0 => AllocResult::Ok,
53 1 => AllocResult::Empty,
54 2 => AllocResult::InvalidAlignment,
55 _ => panic!(),
56 }
57 }
58}