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