Skip to main content

bump_scope/features/
allocator_api2_03.rs

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