Skip to main content

bump_scope/features/
allocator_api2_02.rs

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