1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::{
    alloc::{Layout, LayoutErr as StdLayoutErr},
    fmt,
};

/// Error returned from the allocator.
#[derive(Debug, Clone)]
pub struct AllocErr {
    /// The requested layout.
    pub layout: Layout,
}

impl fmt::Display for AllocErr {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        write!(
            fmtr,
            "the allocator failed for the layout of size {}, align {}",
            self.layout.size(),
            self.layout.align()
        )
    }
}

/// Error caused by invalid size or alignment.
#[derive(Debug, Clone)]
pub struct LayoutErr;

impl fmt::Display for LayoutErr {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        fmtr.write_str("invalid layout parameters")
    }
}

impl From<StdLayoutErr> for LayoutErr {
    fn from(_err: StdLayoutErr) -> Self {
        LayoutErr
    }
}

/// Errors returned by the `RawVec`.
#[derive(Debug, Clone)]
pub enum RawVecErr {
    /// Allocation error.
    Alloc(AllocErr),
    /// Layout error.
    Layout(LayoutErr),
}

impl fmt::Display for RawVecErr {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        match self {
            RawVecErr::Alloc(err) => write!(fmtr, "{}", err),
            RawVecErr::Layout(err) => write!(fmtr, "{}", err),
        }
    }
}

impl From<AllocErr> for RawVecErr {
    fn from(err: AllocErr) -> Self {
        RawVecErr::Alloc(err)
    }
}

impl From<LayoutErr> for RawVecErr {
    fn from(err: LayoutErr) -> Self {
        RawVecErr::Layout(err)
    }
}