Skip to main content

bump_scope/traits/
bump_allocator_core_scope.rs

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