bump_scope/
layout.rs

1use core::{
2    alloc::{Layout, LayoutError},
3    fmt,
4    ops::Deref,
5};
6
7pub(crate) trait LayoutProps: Deref<Target = Layout> + Copy {
8    const ALIGN_IS_CONST: bool;
9    const SIZE_IS_CONST: bool;
10    const SIZE_IS_MULTIPLE_OF_ALIGN: bool;
11}
12
13#[derive(Clone, Copy)]
14pub(crate) struct SizedLayout(Layout);
15
16impl LayoutProps for SizedLayout {
17    const ALIGN_IS_CONST: bool = true;
18    const SIZE_IS_CONST: bool = true;
19    const SIZE_IS_MULTIPLE_OF_ALIGN: bool = true;
20}
21
22impl SizedLayout {
23    #[inline(always)]
24    pub(crate) const fn new<T>() -> Self {
25        Self(Layout::new::<T>())
26    }
27}
28
29impl Deref for SizedLayout {
30    type Target = Layout;
31
32    fn deref(&self) -> &Self::Target {
33        &self.0
34    }
35}
36
37/// This must be `pub` because we use it in `supported_minimum_alignment::Sealed` which is pub.
38/// The current msrv denies us using `pub(crate)` with the error:
39/// ```txt
40/// error[E0446]: crate-private type `ArrayLayout` in public interface
41/// ```
42#[derive(Clone, Copy)]
43pub struct ArrayLayout(Layout);
44
45impl LayoutProps for ArrayLayout {
46    const ALIGN_IS_CONST: bool = true;
47    const SIZE_IS_CONST: bool = false;
48    const SIZE_IS_MULTIPLE_OF_ALIGN: bool = true;
49}
50
51impl ArrayLayout {
52    #[inline(always)]
53    pub(crate) fn for_value<T>(value: &[T]) -> Self {
54        Self(Layout::for_value(value))
55    }
56
57    #[inline(always)]
58    pub(crate) fn array<T>(len: usize) -> Result<Self, LayoutError> {
59        Ok(Self(Layout::array::<T>(len)?))
60    }
61
62    #[inline(always)]
63    pub(crate) const fn from_layout(layout: Layout) -> Result<Self, ArrayLayoutError> {
64        if layout.size() % layout.align() == 0 {
65            Ok(ArrayLayout(layout))
66        } else {
67            Err(ArrayLayoutError)
68        }
69    }
70
71    #[inline(always)]
72    pub(crate) const fn from_size_align(size: usize, align: usize) -> Result<Self, ArrayLayoutError> {
73        match Layout::from_size_align(size, align) {
74            Ok(layout) => Self::from_layout(layout),
75            Err(_) => Err(ArrayLayoutError),
76        }
77    }
78}
79
80impl Deref for ArrayLayout {
81    type Target = Layout;
82
83    fn deref(&self) -> &Self::Target {
84        &self.0
85    }
86}
87
88#[derive(Clone, Copy)]
89pub(crate) struct CustomLayout(pub(crate) Layout);
90
91impl LayoutProps for CustomLayout {
92    const ALIGN_IS_CONST: bool = false;
93    const SIZE_IS_CONST: bool = false;
94    const SIZE_IS_MULTIPLE_OF_ALIGN: bool = false;
95}
96
97impl Deref for CustomLayout {
98    type Target = Layout;
99
100    fn deref(&self) -> &Self::Target {
101        &self.0
102    }
103}
104
105#[derive(Clone, Copy, PartialEq, Eq, Debug)]
106pub(crate) struct ArrayLayoutError;
107
108impl fmt::Display for ArrayLayoutError {
109    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110        f.write_str("invalid parameters to ArrayLayout constructor")
111    }
112}