1#[derive(Copy, Clone)]
3pub struct Layout {
4 #[doc(hidden)]
5 pub size: usize,
6 #[doc(hidden)]
7 pub align: usize,
8}
9
10impl Layout {
11 #[inline]
13 pub fn new<T>() -> Self {
14 return Layout {
15 size: size_of::<T>(),
16 align: align_of::<T>(),
17 };
18 }
19
20 #[inline]
22 pub fn from_size(size: usize) -> Self {
23 return Layout { size, align: 4 };
24 }
25
26 #[inline]
27 pub fn from_size_align(size: usize, align: usize) -> Self {
28 return Layout{ size, align };
29 }
30
31 #[inline]
33 pub fn from_type_array<T>(length: usize) -> Self {
34 return Layout {
35 size: size_of::<T>() * length,
36 align: align_of::<T>(),
37 };
38 }
39
40 #[inline(always)]
42 pub fn align_up(&self, i: usize) -> usize {
43 let p = i + self.align - 1;
44 return p - (p % self.align);
45 }
46}
47
48impl Into<StdLayout> for Layout {
49 fn into(self) -> StdLayout {
50 return StdLayout::from_size_align(self.size, self.align).unwrap();
51 }
52}
53
54impl From<StdLayout> for Layout {
55 fn from(value: StdLayout) -> Self {
56 return Layout::from_size(value.size());
57 }
58}
59
60#[derive(Debug)]
61pub struct LayoutError;
62
63impl Display for LayoutError {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 write!(f, "An error occurred for the requested layout")
66 }
67}
68
69use {
72 std_alloc::alloc::Layout as StdLayout,
73 core::{
74 fmt::{self, Display},
75 mem::{align_of, size_of},
76 },
77};