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