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