Skip to main content

bump_scope/traits/
bump_allocator_core_scope.rs

1use crate::{
2    BaseAllocator, Bump, BumpScope, WithoutDealloc, WithoutShrink,
3    settings::BumpAllocatorSettings,
4    traits::{BumpAllocatorCore, assert_dyn_compatible, assert_implements},
5};
6
7/// A bump allocator scope.
8///
9/// This is a [`BumpAllocatorCore`] which can make allocations that outlive itself.
10/// Specifically, its allocations live for the lifetime `'a`.
11///
12/// # Safety
13///
14/// This trait must only be implemented when allocations live for `'a`.
15/// For example this function must be sound:
16///
17/// ```
18/// # #![expect(dead_code)]
19/// use bump_scope::traits::BumpAllocatorCoreScope;
20/// use core::alloc::Layout;
21///
22/// fn allocate_zeroed_bytes<'a>(allocator: impl BumpAllocatorCoreScope<'a>, len: usize) -> &'a [u8] {
23///     let layout = Layout::array::<u8>(len).unwrap();
24///     let ptr = allocator.allocate_zeroed(layout).unwrap();
25///     unsafe { ptr.as_ref() }
26/// }
27/// ```
28pub unsafe trait BumpAllocatorCoreScope<'a>: BumpAllocatorCore {}
29
30assert_dyn_compatible!(BumpAllocatorCoreScope<'_>);
31
32assert_implements! {
33    [BumpAllocatorCoreScope<'a> + ?Sized]
34
35    BumpScope
36
37    &Bump
38    &BumpScope
39
40    &mut Bump
41    &mut BumpScope
42
43    dyn BumpAllocatorCoreScope
44    &dyn BumpAllocatorCoreScope
45    &mut dyn BumpAllocatorCoreScope
46
47    dyn MutBumpAllocatorCoreScope
48    &dyn MutBumpAllocatorCoreScope
49    &mut dyn MutBumpAllocatorCoreScope
50}
51
52unsafe impl<'a, B: BumpAllocatorCoreScope<'a> + ?Sized> BumpAllocatorCoreScope<'a> for &B {}
53unsafe impl<'a, B: BumpAllocatorCoreScope<'a> + ?Sized> BumpAllocatorCoreScope<'a> for &mut B {}
54
55unsafe impl<'a, B: BumpAllocatorCoreScope<'a>> BumpAllocatorCoreScope<'a> for WithoutDealloc<B> {}
56unsafe impl<'a, B: BumpAllocatorCoreScope<'a>> BumpAllocatorCoreScope<'a> for WithoutShrink<B> {}
57
58unsafe impl<'a, A, S> BumpAllocatorCoreScope<'a> for BumpScope<'a, A, S>
59where
60    A: BaseAllocator<S::GuaranteedAllocated>,
61    S: BumpAllocatorSettings,
62{
63}
64
65unsafe impl<'a, A, S> BumpAllocatorCoreScope<'a> for &'a Bump<A, S>
66where
67    A: BaseAllocator<S::GuaranteedAllocated>,
68    S: BumpAllocatorSettings,
69{
70}
71
72unsafe impl<'a, A, S> BumpAllocatorCoreScope<'a> for &'a mut Bump<A, S>
73where
74    A: BaseAllocator<S::GuaranteedAllocated>,
75    S: BumpAllocatorSettings,
76{
77}