Skip to main content

bump_scope/features/
allocator_api2_04.rs

1use allocator_api2_04::alloc::{AllocError, Allocator};
2
3#[cfg(feature = "alloc")]
4use allocator_api2_04::{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_04::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_04 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::AllocatorApi2V04Compat,
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(AllocatorApi2V04Compat(MyAllocatorApi2Allocator));
50    /// # _ = bump;
51    /// ```
52    struct AllocatorApi2V04Compat for allocator_api2_04
53}
54
55impl_allocator_via_allocator! {
56    self;
57
58    #[cfg(feature = "alloc")]
59    use {self} for crate as allocator_api2_04 impl[] Global
60
61    use {self} for allocator_api2_04 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_04 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_04 as crate impl[A: BumpAllocatorCore] WithoutShrink<A>
74    use {self} for allocator_api2_04 as crate impl[A: BumpAllocatorCore] WithoutDealloc<A>
75}
76
77impl From<AllocError> for CrateAllocError {
78    #[inline(always)]
79    fn from(_: AllocError) -> Self {
80        CrateAllocError
81    }
82}
83
84impl From<CrateAllocError> for AllocError {
85    #[inline(always)]
86    fn from(_: CrateAllocError) -> Self {
87        AllocError
88    }
89}
90
91#[cfg(feature = "alloc")]
92impl<T: ?Sized, A: Allocator> box_like::Sealed for Box<T, A> {
93    type T = T;
94    type A = A;
95
96    #[inline(always)]
97    unsafe fn from_raw_in(ptr: *mut Self::T, allocator: Self::A) -> Self {
98        unsafe { Box::from_raw_in(ptr, allocator) }
99    }
100}
101
102#[cfg(feature = "alloc")]
103impl<T: ?Sized, A: Allocator> BoxLike for Box<T, A> {}