Skip to main content

bump_scope/traits/
macros.rs

1/// Add trait methods as methods to the struct, so users don't have to import
2/// the traits to access the methods and don't have to write `bump.as_scope().alloc(...)`
3/// or `(&bump).alloc(...)` to allocate on a `Bump`.
4///
5/// Would be cool if there was a way to mark the trait impls in a way to make
6/// all the methods available for the struct without importing the trait,
7/// like <https://internals.rust-lang.org/t/fundamental-impl-trait-for-type/19201>.
8macro_rules! forward_methods {
9    (
10        self: $self:ident
11        access: {$access:expr}
12        access_mut: {$access_mut:expr}
13        lifetime: $lifetime:lifetime
14    ) => {
15        /// Forwards to [`BumpAllocatorScope::claim`].
16        #[inline(always)]
17        pub fn claim(&$self) -> BumpClaimGuard<'_, $lifetime, A, S> {
18            BumpAllocatorScope::claim($access)
19        }
20
21        /// Forwards to [`BumpAllocatorCore::is_claimed`].
22        #[must_use]
23        #[inline(always)]
24        pub fn is_claimed(&self) -> bool {
25            BumpAllocatorCore::is_claimed(self)
26        }
27
28        /// Forwards to [`BumpAllocator::scope_guard`].
29        #[inline(always)]
30        pub fn scope_guard(&mut $self) -> BumpScopeGuard<'_, A, S> {
31            BumpAllocator::scope_guard($access_mut)
32        }
33
34        /// Forwards to [`BumpAllocator::scoped`].
35        #[inline(always)]
36        pub fn scoped<R>(&mut $self, f: impl FnOnce(&mut BumpScope<A, S>) -> R) -> R {
37            BumpAllocator::scoped($access_mut, f)
38        }
39
40        /// Forwards to [`BumpAllocator::scoped_aligned`].
41        #[inline(always)]
42        pub fn scoped_aligned<const NEW_MIN_ALIGN: usize, R>(
43            &mut $self,
44            f: impl FnOnce(&mut BumpScope<A, S::WithMinimumAlignment<NEW_MIN_ALIGN>>) -> R,
45        ) -> R
46        where
47            MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
48        {
49            BumpAllocator::scoped_aligned($access_mut, f)
50        }
51
52        /// Forwards to [`BumpAllocatorScope::aligned`].
53        #[inline(always)]
54        pub fn aligned<const NEW_MIN_ALIGN: usize, R>(
55            &mut $self,
56            f: impl FnOnce(&mut BumpScope<$lifetime, A, S::WithMinimumAlignment<NEW_MIN_ALIGN>>) -> R,
57        ) -> R
58        where
59            MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
60        {
61            BumpAllocatorScope::aligned($access_mut, f)
62        }
63
64        /// Forwards to [`BumpAllocatorCore::checkpoint`].
65        #[inline(always)]
66        pub fn checkpoint(&$self) -> Checkpoint {
67            BumpAllocatorCore::checkpoint($access)
68        }
69
70        /// Forwards to [`BumpAllocatorCore::reset_to`].
71        #[inline(always)]
72        pub unsafe fn reset_to(&$self, checkpoint: Checkpoint) {
73            unsafe { BumpAllocatorCore::reset_to($access, checkpoint) }
74        }
75
76        /// Forwards to [`BumpAllocatorScope::allocator`].
77        #[must_use]
78        #[inline(always)]
79        pub fn allocator(&$self) -> &A {
80            BumpAllocatorScope::allocator($access)
81        }
82
83        /// Forwards to [`BumpAllocatorTypedScope::alloc`].
84        #[inline(always)]
85        #[cfg(feature = "panic-on-alloc")]
86        pub fn alloc<T>(&$self, value: T) -> BumpBox<$lifetime, T> {
87            BumpAllocatorTypedScope::alloc($access, value)
88        }
89
90        /// Forwards to [`BumpAllocatorTypedScope::try_alloc`].
91        #[inline(always)]
92        pub fn try_alloc<T>(&$self, value: T) -> Result<BumpBox<$lifetime, T>, AllocError> {
93            BumpAllocatorTypedScope::try_alloc($access, value)
94        }
95
96        /// Forwards to [`BumpAllocatorTypedScope::alloc_with`].
97        #[inline(always)]
98        #[cfg(feature = "panic-on-alloc")]
99        pub fn alloc_with<T>(&$self, f: impl FnOnce() -> T) -> BumpBox<$lifetime, T> {
100            BumpAllocatorTypedScope::alloc_with($access, f)
101        }
102
103        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_with`].
104        #[inline(always)]
105        pub fn try_alloc_with<T>(&$self, f: impl FnOnce() -> T) -> Result<BumpBox<$lifetime, T>, AllocError> {
106            BumpAllocatorTypedScope::try_alloc_with($access, f)
107        }
108
109        /// Forwards to [`BumpAllocatorTypedScope::alloc_default`].
110        #[inline(always)]
111        #[cfg(feature = "panic-on-alloc")]
112        pub fn alloc_default<T: Default>(&$self) -> BumpBox<$lifetime, T> {
113            BumpAllocatorTypedScope::alloc_default($access)
114        }
115
116        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_default`].
117        #[inline(always)]
118        pub fn try_alloc_default<T: Default>(&$self) -> Result<BumpBox<$lifetime, T>, AllocError> {
119            BumpAllocatorTypedScope::try_alloc_default($access)
120        }
121
122        /// Forwards to [`BumpAllocatorTypedScope::alloc_clone`].
123        #[inline(always)]
124        #[cfg(feature = "nightly-clone-to-uninit")]
125        pub fn alloc_clone<T: CloneToUninit + ?Sized>(&$self, value: &T) -> BumpBox<$lifetime, T> {
126            BumpAllocatorTypedScope::alloc_clone($access, value)
127        }
128
129        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_clone`].
130        #[inline(always)]
131        #[cfg(feature = "nightly-clone-to-uninit")]
132        pub fn try_alloc_clone<T: CloneToUninit + ?Sized>(&$self, value: &T) -> Result<BumpBox<$lifetime, T>, AllocError> {
133            BumpAllocatorTypedScope::try_alloc_clone($access, value)
134        }
135
136        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_move`].
137        #[inline(always)]
138        #[cfg(feature = "panic-on-alloc")]
139        pub fn alloc_slice_move<T>(&$self, slice: impl OwnedSlice<Item = T>) -> BumpBox<$lifetime, [T]> {
140            BumpAllocatorTypedScope::alloc_slice_move($access, slice)
141        }
142
143        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_move`].
144        #[inline(always)]
145        pub fn try_alloc_slice_move<T>(&$self, slice: impl OwnedSlice<Item = T>) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
146            BumpAllocatorTypedScope::try_alloc_slice_move($access, slice)
147        }
148
149        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_copy`].
150        #[inline(always)]
151        #[cfg(feature = "panic-on-alloc")]
152        pub fn alloc_slice_copy<T: Copy>(&$self, slice: &[T]) -> BumpBox<$lifetime, [T]> {
153            BumpAllocatorTypedScope::alloc_slice_copy($access, slice)
154        }
155
156        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_copy`].
157        #[inline(always)]
158        pub fn try_alloc_slice_copy<T: Copy>(&$self, slice: &[T]) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
159            BumpAllocatorTypedScope::try_alloc_slice_copy($access, slice)
160        }
161
162        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_clone`].
163        #[inline(always)]
164        #[cfg(feature = "panic-on-alloc")]
165        pub fn alloc_slice_clone<T: Clone>(&$self, slice: &[T]) -> BumpBox<$lifetime, [T]> {
166            BumpAllocatorTypedScope::alloc_slice_clone($access, slice)
167        }
168
169        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_clone`].
170        #[inline(always)]
171        pub fn try_alloc_slice_clone<T: Clone>(&$self, slice: &[T]) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
172            BumpAllocatorTypedScope::try_alloc_slice_clone($access, slice)
173        }
174
175        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_fill`].
176        #[inline(always)]
177        #[cfg(feature = "panic-on-alloc")]
178        pub fn alloc_slice_fill<T: Clone>(&$self, len: usize, value: T) -> BumpBox<$lifetime, [T]> {
179            BumpAllocatorTypedScope::alloc_slice_fill($access, len, value)
180        }
181
182        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_fill`].
183        #[inline(always)]
184        pub fn try_alloc_slice_fill<T: Clone>(&$self, len: usize, value: T) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
185            BumpAllocatorTypedScope::try_alloc_slice_fill($access, len, value)
186        }
187
188        /// Forwards to [`BumpAllocatorTypedScope::alloc_slice_fill_with`].
189        #[inline(always)]
190        #[cfg(feature = "panic-on-alloc")]
191        pub fn alloc_slice_fill_with<T>(&$self, len: usize, f: impl FnMut() -> T) -> BumpBox<$lifetime, [T]> {
192            BumpAllocatorTypedScope::alloc_slice_fill_with($access, len, f)
193        }
194
195        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_slice_fill_with`].
196        #[inline(always)]
197        pub fn try_alloc_slice_fill_with<T>(&$self, len: usize, f: impl FnMut() -> T) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
198            BumpAllocatorTypedScope::try_alloc_slice_fill_with($access, len, f)
199        }
200
201        /// Forwards to [`BumpAllocatorTypedScope::alloc_str`].
202        #[inline(always)]
203        #[cfg(feature = "panic-on-alloc")]
204        pub fn alloc_str(&$self, src: &str) -> BumpBox<$lifetime, str> {
205            BumpAllocatorTypedScope::alloc_str($access, src)
206        }
207
208        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_str`].
209        #[inline(always)]
210        pub fn try_alloc_str(&$self, src: &str) -> Result<BumpBox<$lifetime, str>, AllocError> {
211            BumpAllocatorTypedScope::try_alloc_str($access, src)
212        }
213
214        /// Forwards to [`BumpAllocatorTypedScope::alloc_fmt`].
215        #[inline(always)]
216        #[cfg(feature = "panic-on-alloc")]
217        pub fn alloc_fmt(&$self, args: fmt::Arguments) -> BumpBox<$lifetime, str> {
218            BumpAllocatorTypedScope::alloc_fmt($access, args)
219        }
220
221        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_fmt`].
222        #[inline(always)]
223        pub fn try_alloc_fmt(&$self, args: fmt::Arguments) -> Result<BumpBox<$lifetime, str>, AllocError> {
224            BumpAllocatorTypedScope::try_alloc_fmt($access, args)
225        }
226
227        /// Forwards to [`MutBumpAllocatorTypedScope::alloc_fmt_mut`].
228        #[inline(always)]
229        #[cfg(feature = "panic-on-alloc")]
230        pub fn alloc_fmt_mut(&mut $self, args: fmt::Arguments) -> BumpBox<$lifetime, str> {
231            MutBumpAllocatorTypedScope::alloc_fmt_mut($access_mut, args)
232        }
233
234        /// Forwards to [`MutBumpAllocatorTypedScope::try_alloc_fmt_mut`].
235        #[inline(always)]
236        pub fn try_alloc_fmt_mut(&mut $self, args: fmt::Arguments) -> Result<BumpBox<$lifetime, str>, AllocError> {
237            MutBumpAllocatorTypedScope::try_alloc_fmt_mut($access_mut, args)
238        }
239
240        /// Forwards to [`BumpAllocatorTypedScope::alloc_cstr`].
241        #[inline(always)]
242        #[cfg(feature = "panic-on-alloc")]
243        pub fn alloc_cstr(&$self, src: &CStr) -> &$lifetime CStr {
244            BumpAllocatorTypedScope::alloc_cstr($access, src)
245        }
246
247        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_cstr`].
248        #[inline(always)]
249        pub fn try_alloc_cstr(&$self, src: &CStr) -> Result<&$lifetime CStr, AllocError> {
250            BumpAllocatorTypedScope::try_alloc_cstr($access, src)
251        }
252
253        /// Forwards to [`BumpAllocatorTypedScope::alloc_cstr_from_str`].
254        #[inline(always)]
255        #[cfg(feature = "panic-on-alloc")]
256        pub fn alloc_cstr_from_str(&$self, src: &str) -> &$lifetime CStr {
257            BumpAllocatorTypedScope::alloc_cstr_from_str($access, src)
258        }
259
260        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_cstr_from_str`].
261        #[inline(always)]
262        pub fn try_alloc_cstr_from_str(&$self, src: &str) -> Result<&$lifetime CStr, AllocError> {
263            BumpAllocatorTypedScope::try_alloc_cstr_from_str($access, src)
264        }
265
266        /// Forwards to [`BumpAllocatorTypedScope::alloc_cstr_fmt`].
267        #[inline(always)]
268        #[cfg(feature = "panic-on-alloc")]
269        pub fn alloc_cstr_fmt(&$self, args: fmt::Arguments) -> &$lifetime CStr {
270            BumpAllocatorTypedScope::alloc_cstr_fmt($access, args)
271        }
272
273        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_cstr_fmt`].
274        #[inline(always)]
275        pub fn try_alloc_cstr_fmt(&$self, args: fmt::Arguments) -> Result<&$lifetime CStr, AllocError> {
276            BumpAllocatorTypedScope::try_alloc_cstr_fmt($access, args)
277        }
278
279        /// Forwards to [`MutBumpAllocatorTypedScope::alloc_cstr_fmt_mut`].
280        #[inline(always)]
281        #[cfg(feature = "panic-on-alloc")]
282        pub fn alloc_cstr_fmt_mut(&mut $self, args: fmt::Arguments) -> &$lifetime CStr {
283            MutBumpAllocatorTypedScope::alloc_cstr_fmt_mut($access_mut, args)
284        }
285
286        /// Forwards to [`MutBumpAllocatorTypedScope::try_alloc_cstr_fmt_mut`].
287        #[inline(always)]
288        pub fn try_alloc_cstr_fmt_mut(&mut $self, args: fmt::Arguments) -> Result<&$lifetime CStr, AllocError> {
289            MutBumpAllocatorTypedScope::try_alloc_cstr_fmt_mut($access_mut, args)
290        }
291
292        /// Forwards to [`BumpAllocatorTypedScope::alloc_iter`].
293        #[inline(always)]
294        #[cfg(feature = "panic-on-alloc")]
295        pub fn alloc_iter<T>(&$self, iter: impl IntoIterator<Item = T>) -> BumpBox<$lifetime, [T]> {
296            BumpAllocatorTypedScope::alloc_iter($access, iter)
297        }
298
299        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_iter`].
300        #[inline(always)]
301        pub fn try_alloc_iter<T>(&$self, iter: impl IntoIterator<Item = T>) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
302            BumpAllocatorTypedScope::try_alloc_iter($access, iter)
303        }
304
305        /// Forwards to [`BumpAllocatorTypedScope::alloc_iter_exact`].
306        #[inline(always)]
307        #[cfg(feature = "panic-on-alloc")]
308        pub fn alloc_iter_exact<T, I>(&$self, iter: impl IntoIterator<Item = T, IntoIter = I>) -> BumpBox<$lifetime, [T]>
309        where
310            I: ExactSizeIterator<Item = T>,
311        {
312            BumpAllocatorTypedScope::alloc_iter_exact($access, iter)
313        }
314
315        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_iter_exact`].
316        #[inline(always)]
317        pub fn try_alloc_iter_exact<T, I>(
318            &$self,
319            iter: impl IntoIterator<Item = T, IntoIter = I>,
320        ) -> Result<BumpBox<$lifetime, [T]>, AllocError>
321        where
322            I: ExactSizeIterator<Item = T>,
323        {
324            BumpAllocatorTypedScope::try_alloc_iter_exact($access, iter)
325        }
326
327        /// Forwards to [`MutBumpAllocatorTypedScope::alloc_iter_mut`].
328        #[inline(always)]
329        #[cfg(feature = "panic-on-alloc")]
330        pub fn alloc_iter_mut<T>(&mut $self, iter: impl IntoIterator<Item = T>) -> BumpBox<$lifetime, [T]> {
331            MutBumpAllocatorTypedScope::alloc_iter_mut($access_mut, iter)
332        }
333
334        /// Forwards to [`MutBumpAllocatorTypedScope::try_alloc_iter_mut`].
335        #[inline(always)]
336        pub fn try_alloc_iter_mut<T>(&mut $self, iter: impl IntoIterator<Item = T>) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
337            MutBumpAllocatorTypedScope::try_alloc_iter_mut($access_mut, iter)
338        }
339
340        /// Forwards to [`MutBumpAllocatorTypedScope::alloc_iter_mut_rev`].
341        #[inline(always)]
342        #[cfg(feature = "panic-on-alloc")]
343        pub fn alloc_iter_mut_rev<T>(&mut $self, iter: impl IntoIterator<Item = T>) -> BumpBox<$lifetime, [T]> {
344            MutBumpAllocatorTypedScope::alloc_iter_mut_rev($access_mut, iter)
345        }
346
347        /// Forwards to [`MutBumpAllocatorTypedScope::try_alloc_iter_mut_rev`].
348        #[inline(always)]
349        pub fn try_alloc_iter_mut_rev<T>(&mut $self, iter: impl IntoIterator<Item = T>) -> Result<BumpBox<$lifetime, [T]>, AllocError> {
350            MutBumpAllocatorTypedScope::try_alloc_iter_mut_rev($access_mut, iter)
351        }
352
353        /// Forwards to [`BumpAllocatorTypedScope::alloc_uninit`].
354        #[inline(always)]
355        #[cfg(feature = "panic-on-alloc")]
356        pub fn alloc_uninit<T>(&$self) -> BumpBox<$lifetime, MaybeUninit<T>> {
357            BumpAllocatorTypedScope::alloc_uninit($access)
358        }
359
360        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_uninit`].
361        #[inline(always)]
362        pub fn try_alloc_uninit<T>(&$self) -> Result<BumpBox<$lifetime, MaybeUninit<T>>, AllocError> {
363            BumpAllocatorTypedScope::try_alloc_uninit($access)
364        }
365
366        /// Forwards to [`BumpAllocatorTypedScope::alloc_uninit_slice`].
367        #[inline(always)]
368        #[cfg(feature = "panic-on-alloc")]
369        pub fn alloc_uninit_slice<T>(&$self, len: usize) -> BumpBox<$lifetime, [MaybeUninit<T>]> {
370            BumpAllocatorTypedScope::alloc_uninit_slice($access, len)
371        }
372
373        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_uninit_slice`].
374        #[inline(always)]
375        pub fn try_alloc_uninit_slice<T>(&$self, len: usize) -> Result<BumpBox<$lifetime, [MaybeUninit<T>]>, AllocError> {
376            BumpAllocatorTypedScope::try_alloc_uninit_slice($access, len)
377        }
378
379        /// Forwards to [`BumpAllocatorTypedScope::alloc_uninit_slice_for`].
380        #[inline(always)]
381        #[cfg(feature = "panic-on-alloc")]
382        pub fn alloc_uninit_slice_for<T>(&$self, slice: &[T]) -> BumpBox<$lifetime, [MaybeUninit<T>]> {
383            BumpAllocatorTypedScope::alloc_uninit_slice_for($access, slice)
384        }
385
386        /// Forwards to [`BumpAllocatorTypedScope::try_alloc_uninit_slice_for`].
387        #[inline(always)]
388        pub fn try_alloc_uninit_slice_for<T>(&$self, slice: &[T]) -> Result<BumpBox<$lifetime, [MaybeUninit<T>]>, AllocError> {
389            BumpAllocatorTypedScope::try_alloc_uninit_slice_for($access, slice)
390        }
391
392        /// Forwards to [`BumpAllocatorTyped::dealloc`].
393        #[inline(always)]
394        pub fn dealloc<T: ?Sized>(&$self, boxed: BumpBox<T>) {
395            BumpAllocatorTyped::dealloc($access, boxed);
396        }
397
398        /// Forwards to [`BumpAllocatorTyped::reserve`].
399        #[inline(always)]
400        #[cfg(feature = "panic-on-alloc")]
401        pub fn reserve(&$self, additional: usize) {
402            BumpAllocatorTyped::reserve($access, additional);
403        }
404
405        /// Forwards to [`BumpAllocatorTyped::try_reserve`].
406        #[inline(always)]
407        pub fn try_reserve(&$self, additional: usize) -> Result<(), AllocError> {
408            BumpAllocatorTyped::try_reserve($access, additional)
409        }
410    };
411}
412
413pub(crate) use forward_methods;