bump-scope 2.3.0

A fast bump allocator that supports allocation scopes / checkpoints. Aka an arena for values of arbitrary types.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/// Add trait methods as methods to the struct, so users don't have to import
/// the traits to access the methods and don't have to write `bump.as_scope().alloc(...)`
/// or `(&bump).alloc(...)` to allocate on a `Bump`.
///
/// Would be cool if there was a way to mark the trait impls in a way to make
/// all the methods available for the struct without importing the trait,
/// like <https://internals.rust-lang.org/t/fundamental-impl-trait-for-type/19201>.
macro_rules! forward_methods {
    (
        self: $self:ident
        access: {$access:expr}
        access_mut: {$access_mut:expr}
        lifetime: $lifetime:lifetime
    ) => {
        /// Forwards to [`BumpAllocatorScope::allocator`].
        #[must_use]
        #[inline(always)]
        pub fn allocator(&$self) -> Option<&$lifetime A> {
            BumpAllocatorScope::allocator($access)
        }

        /// Forwards to [`BumpAllocatorScope::claim`].
        #[inline(always)]
        pub fn claim(&$self) -> BumpClaimGuard<'_, $lifetime, A, S> {
            BumpAllocatorScope::claim($access)
        }

        /// Forwards to [`BumpAllocatorCore::is_claimed`].
        #[must_use]
        #[inline(always)]
        pub fn is_claimed(&self) -> bool {
            BumpAllocatorCore::is_claimed(self)
        }

        /// Forwards to [`BumpAllocator::scope_guard`].
        #[inline(always)]
        pub fn scope_guard(&mut $self) -> BumpScopeGuard<'_, A, S> {
            BumpAllocator::scope_guard($access_mut)
        }

        /// Forwards to [`BumpAllocator::scoped`].
        #[inline(always)]
        pub fn scoped<R>(&mut $self, f: impl FnOnce(&mut BumpScope<A, S>) -> R) -> R {
            BumpAllocator::scoped($access_mut, f)
        }

        /// Forwards to [`BumpAllocator::scoped_aligned`].
        #[inline(always)]
        pub fn scoped_aligned<const NEW_MIN_ALIGN: usize, R>(
            &mut $self,
            f: impl FnOnce(&mut BumpScope<A, S::WithMinimumAlignment<NEW_MIN_ALIGN>>) -> R,
        ) -> R
        where
            MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
        {
            BumpAllocator::scoped_aligned($access_mut, f)
        }

        /// Forwards to [`BumpAllocatorScope::aligned`].
        #[inline(always)]
        pub fn aligned<const NEW_MIN_ALIGN: usize, R>(
            &mut $self,
            f: impl FnOnce(&mut BumpScope<$lifetime, A, S::WithMinimumAlignment<NEW_MIN_ALIGN>>) -> R,
        ) -> R
        where
            MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
        {
            BumpAllocatorScope::aligned($access_mut, f)
        }

        /// Forwards to [`BumpAllocatorCore::checkpoint`].
        #[inline(always)]
        pub fn checkpoint(&$self) -> Checkpoint {
            BumpAllocatorCore::checkpoint($access)
        }

        /// Forwards to [`BumpAllocatorCore::reset_to`].
        #[inline(always)]
        pub unsafe fn reset_to(&$self, checkpoint: Checkpoint) {
            unsafe { BumpAllocatorCore::reset_to($access, checkpoint) }
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc<T>(&$self, value: T) -> BumpBox<$lifetime, T> {
            BumpAllocatorTypedScope::alloc($access, value)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc`].
        #[inline(always)]
        pub fn try_alloc<T>(&$self, value: T) -> Result<BumpBox<$lifetime, T>, AllocError> {
            BumpAllocatorTypedScope::try_alloc($access, value)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_with`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_with<T>(&$self, f: impl FnOnce() -> T) -> BumpBox<$lifetime, T> {
            BumpAllocatorTypedScope::alloc_with($access, f)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_with`].
        #[inline(always)]
        pub fn try_alloc_with<T>(&$self, f: impl FnOnce() -> T) -> Result<BumpBox<$lifetime, T>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_with($access, f)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_default`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_default<T: Default>(&$self) -> BumpBox<$lifetime, T> {
            BumpAllocatorTypedScope::alloc_default($access)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_default`].
        #[inline(always)]
        pub fn try_alloc_default<T: Default>(&$self) -> Result<BumpBox<$lifetime, T>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_default($access)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_clone`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        #[cfg(feature = "nightly-clone-to-uninit")]
        pub fn alloc_clone<T: CloneToUninit + ?Sized>(&$self, value: &T) -> BumpBox<$lifetime, T> {
            BumpAllocatorTypedScope::alloc_clone($access, value)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_clone`].
        #[inline(always)]
        #[cfg(feature = "nightly-clone-to-uninit")]
        pub fn try_alloc_clone<T: CloneToUninit + ?Sized>(&$self, value: &T) -> Result<BumpBox<$lifetime, T>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_clone($access, value)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_move`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_slice_move<T>(&$self, slice: impl OwnedSlice<Item = T>) -> BumpBox<$lifetime, [T]> {
            BumpAllocatorTypedScope::alloc_slice_move($access, slice)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_move`].
        #[inline(always)]
        pub fn try_alloc_slice_move<T>(&$self, slice: impl OwnedSlice<Item = T>) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_slice_move($access, slice)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_copy`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_slice_copy<T: Copy>(&$self, slice: &[T]) -> BumpBox<$lifetime, [T]> {
            BumpAllocatorTypedScope::alloc_slice_copy($access, slice)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_copy`].
        #[inline(always)]
        pub fn try_alloc_slice_copy<T: Copy>(&$self, slice: &[T]) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_slice_copy($access, slice)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_clone`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_slice_clone<T: Clone>(&$self, slice: &[T]) -> BumpBox<$lifetime, [T]> {
            BumpAllocatorTypedScope::alloc_slice_clone($access, slice)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_clone`].
        #[inline(always)]
        pub fn try_alloc_slice_clone<T: Clone>(&$self, slice: &[T]) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_slice_clone($access, slice)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_fill`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_slice_fill<T: Clone>(&$self, len: usize, value: T) -> BumpBox<$lifetime, [T]> {
            BumpAllocatorTypedScope::alloc_slice_fill($access, len, value)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_fill`].
        #[inline(always)]
        pub fn try_alloc_slice_fill<T: Clone>(&$self, len: usize, value: T) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_slice_fill($access, len, value)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_fill_with`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_slice_fill_with<T>(&$self, len: usize, f: impl FnMut() -> T) -> BumpBox<$lifetime, [T]> {
            BumpAllocatorTypedScope::alloc_slice_fill_with($access, len, f)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_fill_with`].
        #[inline(always)]
        pub fn try_alloc_slice_fill_with<T>(&$self, len: usize, f: impl FnMut() -> T) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_slice_fill_with($access, len, f)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_str`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_str(&$self, src: &str) -> BumpBox<$lifetime, str> {
            BumpAllocatorTypedScope::alloc_str($access, src)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_str`].
        #[inline(always)]
        pub fn try_alloc_str(&$self, src: &str) -> Result<BumpBox<$lifetime, str>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_str($access, src)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_fmt`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_fmt(&$self, args: fmt::Arguments) -> BumpBox<$lifetime, str> {
            BumpAllocatorTypedScope::alloc_fmt($access, args)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_fmt`].
        #[inline(always)]
        pub fn try_alloc_fmt(&$self, args: fmt::Arguments) -> Result<BumpBox<$lifetime, str>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_fmt($access, args)
        }

        /// Forwards to [`MutBumpAllocatorTypedScope::alloc_fmt_mut`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_fmt_mut(&mut $self, args: fmt::Arguments) -> BumpBox<$lifetime, str> {
            MutBumpAllocatorTypedScope::alloc_fmt_mut($access_mut, args)
        }

        /// Forwards to [`MutBumpAllocatorTypedScope::try_alloc_fmt_mut`].
        #[inline(always)]
        pub fn try_alloc_fmt_mut(&mut $self, args: fmt::Arguments) -> Result<BumpBox<$lifetime, str>, AllocError> {
            MutBumpAllocatorTypedScope::try_alloc_fmt_mut($access_mut, args)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_cstr`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_cstr(&$self, src: &CStr) -> &$lifetime CStr {
            BumpAllocatorTypedScope::alloc_cstr($access, src)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_cstr`].
        #[inline(always)]
        pub fn try_alloc_cstr(&$self, src: &CStr) -> Result<&$lifetime CStr, AllocError> {
            BumpAllocatorTypedScope::try_alloc_cstr($access, src)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_cstr_from_str`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_cstr_from_str(&$self, src: &str) -> &$lifetime CStr {
            BumpAllocatorTypedScope::alloc_cstr_from_str($access, src)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_cstr_from_str`].
        #[inline(always)]
        pub fn try_alloc_cstr_from_str(&$self, src: &str) -> Result<&$lifetime CStr, AllocError> {
            BumpAllocatorTypedScope::try_alloc_cstr_from_str($access, src)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_cstr_fmt`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_cstr_fmt(&$self, args: fmt::Arguments) -> &$lifetime CStr {
            BumpAllocatorTypedScope::alloc_cstr_fmt($access, args)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_cstr_fmt`].
        #[inline(always)]
        pub fn try_alloc_cstr_fmt(&$self, args: fmt::Arguments) -> Result<&$lifetime CStr, AllocError> {
            BumpAllocatorTypedScope::try_alloc_cstr_fmt($access, args)
        }

        /// Forwards to [`MutBumpAllocatorTypedScope::alloc_cstr_fmt_mut`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_cstr_fmt_mut(&mut $self, args: fmt::Arguments) -> &$lifetime CStr {
            MutBumpAllocatorTypedScope::alloc_cstr_fmt_mut($access_mut, args)
        }

        /// Forwards to [`MutBumpAllocatorTypedScope::try_alloc_cstr_fmt_mut`].
        #[inline(always)]
        pub fn try_alloc_cstr_fmt_mut(&mut $self, args: fmt::Arguments) -> Result<&$lifetime CStr, AllocError> {
            MutBumpAllocatorTypedScope::try_alloc_cstr_fmt_mut($access_mut, args)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_iter`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_iter<T>(&$self, iter: impl IntoIterator<Item = T>) -> BumpBox<$lifetime, [T]> {
            BumpAllocatorTypedScope::alloc_iter($access, iter)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_iter`].
        #[inline(always)]
        pub fn try_alloc_iter<T>(&$self, iter: impl IntoIterator<Item = T>) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_iter($access, iter)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_iter_exact`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_iter_exact<T, I>(&$self, iter: impl IntoIterator<Item = T, IntoIter = I>) -> BumpBox<$lifetime, [T]>
        where
            I: ExactSizeIterator<Item = T>,
        {
            BumpAllocatorTypedScope::alloc_iter_exact($access, iter)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_iter_exact`].
        #[inline(always)]
        pub fn try_alloc_iter_exact<T, I>(
            &$self,
            iter: impl IntoIterator<Item = T, IntoIter = I>,
        ) -> Result<BumpBox<$lifetime, [T]>, AllocError>
        where
            I: ExactSizeIterator<Item = T>,
        {
            BumpAllocatorTypedScope::try_alloc_iter_exact($access, iter)
        }

        /// Forwards to [`MutBumpAllocatorTypedScope::alloc_iter_mut`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_iter_mut<T>(&mut $self, iter: impl IntoIterator<Item = T>) -> BumpBox<$lifetime, [T]> {
            MutBumpAllocatorTypedScope::alloc_iter_mut($access_mut, iter)
        }

        /// Forwards to [`MutBumpAllocatorTypedScope::try_alloc_iter_mut`].
        #[inline(always)]
        pub fn try_alloc_iter_mut<T>(&mut $self, iter: impl IntoIterator<Item = T>) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
            MutBumpAllocatorTypedScope::try_alloc_iter_mut($access_mut, iter)
        }

        /// Forwards to [`MutBumpAllocatorTypedScope::alloc_iter_mut_rev`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_iter_mut_rev<T>(&mut $self, iter: impl IntoIterator<Item = T>) -> BumpBox<$lifetime, [T]> {
            MutBumpAllocatorTypedScope::alloc_iter_mut_rev($access_mut, iter)
        }

        /// Forwards to [`MutBumpAllocatorTypedScope::try_alloc_iter_mut_rev`].
        #[inline(always)]
        pub fn try_alloc_iter_mut_rev<T>(&mut $self, iter: impl IntoIterator<Item = T>) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
            MutBumpAllocatorTypedScope::try_alloc_iter_mut_rev($access_mut, iter)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_uninit`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_uninit<T>(&$self) -> BumpBox<$lifetime, MaybeUninit<T>> {
            BumpAllocatorTypedScope::alloc_uninit($access)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_uninit`].
        #[inline(always)]
        pub fn try_alloc_uninit<T>(&$self) -> Result<BumpBox<$lifetime, MaybeUninit<T>>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_uninit($access)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_uninit_slice`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_uninit_slice<T>(&$self, len: usize) -> BumpBox<$lifetime, [MaybeUninit<T>]> {
            BumpAllocatorTypedScope::alloc_uninit_slice($access, len)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_uninit_slice`].
        #[inline(always)]
        pub fn try_alloc_uninit_slice<T>(&$self, len: usize) -> Result<BumpBox<$lifetime, [MaybeUninit<T>]>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_uninit_slice($access, len)
        }

        /// Forwards to [`BumpAllocatorTypedScope::alloc_uninit_slice_for`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn alloc_uninit_slice_for<T>(&$self, slice: &[T]) -> BumpBox<$lifetime, [MaybeUninit<T>]> {
            BumpAllocatorTypedScope::alloc_uninit_slice_for($access, slice)
        }

        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_uninit_slice_for`].
        #[inline(always)]
        pub fn try_alloc_uninit_slice_for<T>(&$self, slice: &[T]) -> Result<BumpBox<$lifetime, [MaybeUninit<T>]>, AllocError> {
            BumpAllocatorTypedScope::try_alloc_uninit_slice_for($access, slice)
        }

        /// Forwards to [`BumpAllocatorTyped::dealloc`].
        #[inline(always)]
        pub fn dealloc<T: ?Sized>(&$self, boxed: BumpBox<T>) {
            BumpAllocatorTyped::dealloc($access, boxed);
        }

        /// Forwards to [`BumpAllocatorTyped::reserve`].
        #[inline(always)]
        #[cfg(feature = "panic-on-alloc")]
        pub fn reserve(&$self, additional: usize) {
            BumpAllocatorTyped::reserve($access, additional);
        }

        /// Forwards to [`BumpAllocatorTyped::try_reserve`].
        #[inline(always)]
        pub fn try_reserve(&$self, additional: usize) -> Result<(), AllocError> {
            BumpAllocatorTyped::try_reserve($access, additional)
        }
    };
}

pub(crate) use forward_methods;