Skip to main content

bump_scope/features/
nightly_allocator_api.rs

1use core::alloc::{AllocError, Allocator};
2
3#[cfg(feature = "alloc")]
4use alloc_crate::{alloc::Global, boxed::Box};
5
6use crate::{
7    Bump, BumpScope, WithoutDealloc, WithoutShrink,
8    alloc::{AllocError as CrateAllocError, Allocator as CrateAllocator, BoxLike, box_like},
9    settings::BumpAllocatorSettings,
10    traits::BumpAllocatorCore,
11};
12
13use super::allocator_util::{allocator_compat_wrapper, impl_allocator_via_allocator};
14
15allocator_compat_wrapper! {
16    /// Wraps an <code>alloc::alloc::[Allocator](core::alloc::Allocator)</code> to implement
17    /// <code>bump_scope::alloc::[Allocator](crate::alloc::Allocator)</code> and vice versa.
18    ///
19    /// # Example
20    ///
21    /// ```
22    /// # extern crate alloc;
23    /// # use core::{alloc::Layout, ptr::NonNull};
24    /// # use alloc::alloc::{AllocError, Global};
25    /// use alloc::alloc::Allocator;
26    ///
27    /// use bump_scope::{Bump, alloc::compat::AllocatorNightlyCompat};
28    ///
29    /// #[derive(Clone)]
30    /// struct MyNightlyAllocator;
31    ///
32    /// unsafe impl Allocator for MyNightlyAllocator {
33    /// # /*
34    ///     ...
35    /// # */
36    /// #   fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
37    /// #       <Global as Allocator>::allocate(&Global, layout)
38    /// #   }
39    /// #
40    /// #   unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
41    /// #       <Global as Allocator>::deallocate(&Global, ptr, layout)
42    /// #   }
43    /// }
44    ///
45    /// let bump: Bump<_> = Bump::new_in(AllocatorNightlyCompat(MyNightlyAllocator));
46    /// # _ = bump;
47    /// ```
48    struct AllocatorNightlyCompat for core
49}
50
51impl_allocator_via_allocator! {
52    self;
53
54    #[cfg(feature = "alloc")]
55    use {self} for crate as core impl[] Global
56
57    use {self} for core as crate impl[A, S] Bump<A, S>
58    where [
59        A: CrateAllocator,
60        S: BumpAllocatorSettings,
61    ]
62
63    use {self} for core as crate impl[A, S] BumpScope<'_, A, S>
64    where [
65        A: CrateAllocator,
66        S: BumpAllocatorSettings,
67    ]
68
69    use {self} for core as crate impl[A: BumpAllocatorCore] WithoutShrink<A>
70    use {self} for core as crate impl[A: BumpAllocatorCore] WithoutDealloc<A>
71}
72
73impl From<AllocError> for CrateAllocError {
74    #[inline(always)]
75    fn from(_: AllocError) -> Self {
76        CrateAllocError
77    }
78}
79
80impl From<CrateAllocError> for AllocError {
81    #[inline(always)]
82    fn from(_: CrateAllocError) -> Self {
83        AllocError
84    }
85}
86
87impl<T: ?Sized, A: Allocator> box_like::Sealed for Box<T, A> {
88    type T = T;
89    type A = A;
90
91    #[inline(always)]
92    unsafe fn from_raw_in(ptr: *mut Self::T, allocator: Self::A) -> Self {
93        unsafe { Box::from_raw_in(ptr, allocator) }
94    }
95}
96
97impl<T: ?Sized, A: Allocator> BoxLike for Box<T, A> {}