tux_owned_alloc/
err.rs

1use std::{
2    alloc::{Layout, LayoutError as StdLayoutErr},
3    fmt,
4};
5
6/// Error returned from the allocator.
7#[derive(Debug, Clone)]
8pub struct AllocErr {
9    /// The requested layout.
10    pub layout: Layout,
11}
12
13impl fmt::Display for AllocErr {
14    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
15        write!(
16            fmtr,
17            "the allocator failed for the layout of size {}, align {}",
18            self.layout.size(),
19            self.layout.align()
20        )
21    }
22}
23
24/// Error caused by invalid size or alignment.
25#[derive(Debug, Clone)]
26pub struct LayoutErr;
27
28impl fmt::Display for LayoutErr {
29    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
30        fmtr.write_str("invalid layout parameters")
31    }
32}
33
34impl From<StdLayoutErr> for LayoutErr {
35    fn from(_err: StdLayoutErr) -> Self {
36        LayoutErr
37    }
38}
39
40/// Errors returned by the `RawVec`.
41#[derive(Debug, Clone)]
42pub enum RawVecErr {
43    /// Allocation error.
44    Alloc(AllocErr),
45    /// Layout error.
46    Layout(LayoutErr),
47}
48
49impl fmt::Display for RawVecErr {
50    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
51        match self {
52            RawVecErr::Alloc(err) => write!(fmtr, "{}", err),
53            RawVecErr::Layout(err) => write!(fmtr, "{}", err),
54        }
55    }
56}
57
58impl From<AllocErr> for RawVecErr {
59    fn from(err: AllocErr) -> Self {
60        RawVecErr::Alloc(err)
61    }
62}
63
64impl From<LayoutErr> for RawVecErr {
65    fn from(err: LayoutErr) -> Self {
66        RawVecErr::Layout(err)
67    }
68}