1use core::{alloc, convert};
2use core::num::NonZeroUsize;
3
4#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct Layout(alloc::Layout);
10
11#[derive(Clone, Copy, Debug, PartialEq)]
13pub struct NonZeroLayout(Layout);
14
15#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
16pub struct EmptyLayoutError;
17
18impl Layout {
19 pub fn new<T>() -> Self {
21 Layout(alloc::Layout::new::<T>())
22 }
23
24 pub fn size(&self) -> usize {
26 self.0.size()
27 }
28
29 pub fn align(&self) -> usize {
31 self.0.align()
32 }
33}
34
35impl NonZeroLayout {
36 pub fn new<T>() -> Option<Self> {
41 Self::from_layout(Layout::new::<T>())
42 }
43
44 pub fn from_layout(layout: Layout) -> Option<Self> {
46 if layout.size() == 0 {
47 None
48 } else {
49 Some(NonZeroLayout(layout))
50 }
51 }
52
53 pub fn size(&self) -> NonZeroUsize {
55 NonZeroUsize::new(self.0.size()).unwrap()
56 }
57
58 pub fn align(&self) -> usize {
60 self.0.align()
61 }
62}
63
64impl From<Layout> for alloc::Layout {
65 fn from(layout: Layout) -> alloc::Layout {
66 layout.0
67 }
68}
69
70impl From<NonZeroLayout> for alloc::Layout {
71 fn from(layout: NonZeroLayout) -> alloc::Layout {
72 layout.0.into()
73 }
74}
75
76impl From<alloc::Layout> for Layout {
77 fn from(layout: alloc::Layout) -> Layout {
78 Layout(layout)
79 }
80}
81
82impl convert::TryFrom<Layout> for NonZeroLayout {
83 type Error = EmptyLayoutError;
84
85 fn try_from(layout: Layout) -> Result<Self, EmptyLayoutError> {
86 NonZeroLayout::from_layout(layout).ok_or(EmptyLayoutError)
87 }
88}
89
90impl convert::TryFrom<alloc::Layout> for NonZeroLayout {
91 type Error = EmptyLayoutError;
92
93 fn try_from(layout: alloc::Layout) -> Result<Self, EmptyLayoutError> {
94 NonZeroLayout::try_from(Layout::from(layout))
95 }
96}