bump_scope/
error_behavior.rs1use core::alloc::Layout;
2
3use crate::{
4 NonNull,
5 alloc::AllocError,
6 traits::{BumpAllocatorTyped, MutBumpAllocatorTyped},
7};
8
9#[cfg(feature = "panic-on-alloc")]
10use crate::{Infallible, capacity_overflow, format_trait_error, handle_alloc_error};
11
12pub(crate) trait ErrorBehavior: Sized {
13 #[cfg(feature = "panic-on-alloc")]
14 const PANICS_ON_ALLOC: bool;
15
16 fn allocation(layout: Layout) -> Self;
17 fn capacity_overflow() -> Self;
18 fn claimed() -> Self;
19 fn fixed_size_vector_is_full() -> Self;
20 fn fixed_size_vector_no_space(amount: usize) -> Self;
21 fn invalid_slice_layout() -> Self;
22 fn format_trait_error() -> Self;
23 #[expect(dead_code)]
24 fn allocate_layout(allocator: &impl BumpAllocatorTyped, layout: Layout) -> Result<NonNull<u8>, Self>;
25 #[expect(dead_code)]
26 fn allocate_sized<T>(allocator: &impl BumpAllocatorTyped) -> Result<NonNull<T>, Self>;
27 fn allocate_slice<T>(allocator: &impl BumpAllocatorTyped, len: usize) -> Result<NonNull<T>, Self>;
28 unsafe fn prepare_slice_allocation<T>(
29 allocator: &mut impl MutBumpAllocatorTyped,
30 len: usize,
31 ) -> Result<NonNull<[T]>, Self>;
32 unsafe fn prepare_slice_allocation_rev<T>(
33 allocator: &mut impl MutBumpAllocatorTyped,
34 len: usize,
35 ) -> Result<(NonNull<T>, usize), Self>;
36}
37
38#[cfg(feature = "panic-on-alloc")]
39impl ErrorBehavior for Infallible {
40 #[cfg(feature = "panic-on-alloc")]
41 const PANICS_ON_ALLOC: bool = true;
42
43 #[inline(always)]
44 fn allocation(layout: Layout) -> Self {
45 handle_alloc_error(layout)
46 }
47
48 #[inline(always)]
49 fn capacity_overflow() -> Self {
50 capacity_overflow()
51 }
52
53 #[inline(always)]
54 fn claimed() -> Self {
55 panic::claimed()
56 }
57
58 #[inline(always)]
59 fn fixed_size_vector_is_full() -> Self {
60 panic::fixed_size_vector_is_full()
61 }
62
63 #[inline(always)]
64 fn fixed_size_vector_no_space(amount: usize) -> Self {
65 panic::fixed_size_vector_no_space(amount)
66 }
67
68 #[inline(always)]
69 fn invalid_slice_layout() -> Self {
70 panic::invalid_slice_layout()
71 }
72
73 #[inline(always)]
74 fn format_trait_error() -> Self {
75 format_trait_error()
76 }
77
78 #[inline(always)]
79 fn allocate_layout(allocator: &impl BumpAllocatorTyped, layout: Layout) -> Result<NonNull<u8>, Self> {
80 Ok(allocator.allocate_layout(layout))
81 }
82
83 #[inline(always)]
84 fn allocate_sized<T>(allocator: &impl BumpAllocatorTyped) -> Result<NonNull<T>, Self> {
85 Ok(allocator.allocate_sized::<T>())
86 }
87
88 #[inline(always)]
89 fn allocate_slice<T>(allocator: &impl BumpAllocatorTyped, len: usize) -> Result<NonNull<T>, Self> {
90 Ok(allocator.allocate_slice::<T>(len))
91 }
92
93 #[inline(always)]
94 unsafe fn prepare_slice_allocation<T>(
95 allocator: &mut impl MutBumpAllocatorTyped,
96 len: usize,
97 ) -> Result<NonNull<[T]>, Self> {
98 Ok(allocator.prepare_slice_allocation::<T>(len))
99 }
100
101 #[inline(always)]
102 unsafe fn prepare_slice_allocation_rev<T>(
103 allocator: &mut impl MutBumpAllocatorTyped,
104 len: usize,
105 ) -> Result<(NonNull<T>, usize), Self> {
106 Ok(allocator.prepare_slice_allocation_rev::<T>(len))
107 }
108}
109
110impl ErrorBehavior for AllocError {
111 #[cfg(feature = "panic-on-alloc")]
112 const PANICS_ON_ALLOC: bool = false;
113
114 #[inline(always)]
115 fn allocation(_: Layout) -> Self {
116 Self
117 }
118
119 #[inline(always)]
120 fn capacity_overflow() -> Self {
121 Self
122 }
123
124 #[inline(always)]
125 fn claimed() -> Self {
126 Self
127 }
128
129 #[inline(always)]
130 fn fixed_size_vector_is_full() -> Self {
131 Self
132 }
133
134 #[inline(always)]
135 fn fixed_size_vector_no_space(amount: usize) -> Self {
136 let _ = amount;
137 Self
138 }
139
140 #[inline(always)]
141 fn invalid_slice_layout() -> Self {
142 Self
143 }
144
145 #[inline(always)]
146 fn format_trait_error() -> Self {
147 Self
148 }
149
150 #[inline(always)]
151 fn allocate_layout(allocator: &impl BumpAllocatorTyped, layout: Layout) -> Result<NonNull<u8>, Self> {
152 allocator.try_allocate_layout(layout)
153 }
154
155 #[inline(always)]
156 fn allocate_sized<T>(allocator: &impl BumpAllocatorTyped) -> Result<NonNull<T>, Self> {
157 allocator.try_allocate_sized::<T>()
158 }
159
160 #[inline(always)]
161 fn allocate_slice<T>(allocator: &impl BumpAllocatorTyped, len: usize) -> Result<NonNull<T>, Self> {
162 allocator.try_allocate_slice::<T>(len)
163 }
164
165 #[inline(always)]
166 unsafe fn prepare_slice_allocation<T>(
167 allocator: &mut impl MutBumpAllocatorTyped,
168 len: usize,
169 ) -> Result<NonNull<[T]>, Self> {
170 allocator.try_prepare_slice_allocation::<T>(len)
171 }
172
173 #[inline(always)]
174 unsafe fn prepare_slice_allocation_rev<T>(
175 allocator: &mut impl MutBumpAllocatorTyped,
176 len: usize,
177 ) -> Result<(NonNull<T>, usize), Self> {
178 allocator.try_prepare_slice_allocation_rev::<T>(len)
179 }
180}
181
182pub(crate) mod panic {
183 #[cold]
184 #[inline(never)]
185 pub(crate) fn claimed() -> ! {
186 panic!("bump allocator is claimed");
187 }
188
189 #[cold]
190 #[inline(never)]
191 pub(crate) fn unallocated() -> ! {
192 panic!("bump allocator is unallocated");
193 }
194
195 #[cold]
196 #[inline(never)]
197 #[cfg(feature = "panic-on-alloc")]
198 pub(crate) fn fixed_size_vector_is_full() -> ! {
199 panic!("fixed size vector is full");
200 }
201
202 #[cold]
203 #[inline(never)]
204 #[cfg(feature = "panic-on-alloc")]
205 pub(crate) fn fixed_size_vector_no_space(amount: usize) -> ! {
206 panic!("fixed size vector does not have space for {amount} more elements");
207 }
208
209 #[cold]
210 #[inline(never)]
211 #[cfg(feature = "panic-on-alloc")]
212 pub(crate) const fn invalid_slice_layout() -> ! {
213 panic!("invalid slice layout");
214 }
215}