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