1use core::marker::PhantomData;
2#[cfg(feature = "alloc")]
3use core::ptr::NonNull;
4
5use super::{Alloc, AllocError, Allocator};
6#[cfg(feature = "alloc")]
7use super::{Global, GlobalAlloc, GlobalAllocator};
8#[cfg(not(feature = "alloc"))]
9use super::{Slice, SliceAlloc};
10
11pub const DEFAULT_ARRAY_BUFFER: usize = 4096;
16
17macro_rules! implement {
18 ($id:ident, $ty:ty, $raw_vec:ty, $raw:ty) => {
19 #[repr(transparent)]
28 pub struct $id<'buf, const BUF: usize> {
29 inner: $ty,
30 _marker: PhantomData<&'buf mut [u8]>,
31 }
32
33 impl<'buf, const BUF: usize> $id<'buf, BUF> {
34 #[inline]
35 pub(super) const fn new(inner: $ty) -> Self {
36 Self {
37 inner,
38 _marker: PhantomData,
39 }
40 }
41 }
42
43 pub struct DefaultAlloc<'a, T, const BUF: usize> {
52 inner: $raw,
53 _marker: PhantomData<&'a ()>,
54 }
55 };
56}
57
58#[cfg(feature = "alloc")]
59implement!(DefaultAllocator, Global, SystemAlloc<T>, GlobalAlloc<T>);
60
61#[cfg(not(feature = "alloc"))]
62implement!(
63 DefaultAllocator,
64 Slice<'buf>,
65 SliceAlloc<'a, T>,
66 SliceAlloc<'a, T>
67);
68
69#[cfg(feature = "alloc")]
70unsafe impl<const BUF: usize> GlobalAllocator for &DefaultAllocator<'_, BUF> {
71 #[inline]
72 fn __do_not_implement() {}
73
74 #[inline]
75 fn new() -> Self {
76 &const { DefaultAllocator::new(Global::new()) }
77 }
78
79 #[inline]
80 fn clone_alloc<T>(alloc: &Self::Alloc<T>) -> Self::Alloc<T> {
81 DefaultAlloc {
82 inner: <Global as GlobalAllocator>::clone_alloc(&alloc.inner),
83 _marker: PhantomData,
84 }
85 }
86
87 #[inline]
88 fn slice_from_raw_parts<T>(ptr: NonNull<T>, len: usize) -> Self::Alloc<T> {
89 DefaultAlloc {
90 inner: Global::slice_from_raw_parts(ptr, len),
91 _marker: PhantomData,
92 }
93 }
94}
95
96unsafe impl<'a, const BUF: usize> Allocator for &'a DefaultAllocator<'_, BUF> {
97 #[inline]
98 fn __do_not_implement() {}
99
100 #[cfg(feature = "alloc")]
101 const IS_GLOBAL: bool = true;
102
103 #[cfg(not(feature = "alloc"))]
104 const IS_GLOBAL: bool = false;
105
106 type Alloc<T> = DefaultAlloc<'a, T, BUF>;
107
108 #[inline]
109 fn alloc<T>(self, value: T) -> Result<Self::Alloc<T>, AllocError> {
110 Ok(DefaultAlloc {
111 inner: self.inner.alloc(value)?,
112 _marker: PhantomData,
113 })
114 }
115
116 #[inline]
117 fn alloc_empty<T>(self) -> Self::Alloc<T> {
118 DefaultAlloc {
119 inner: self.inner.alloc_empty(),
120 _marker: PhantomData,
121 }
122 }
123}
124
125impl<T, const BUF: usize> Alloc<T> for DefaultAlloc<'_, T, BUF> {
126 #[inline]
127 fn as_ptr(&self) -> *const T {
128 Alloc::as_ptr(&self.inner)
129 }
130
131 #[inline]
132 fn as_mut_ptr(&mut self) -> *mut T {
133 Alloc::as_mut_ptr(&mut self.inner)
134 }
135
136 #[inline]
137 fn capacity(&self) -> usize {
138 self.inner.capacity()
139 }
140
141 #[inline]
142 fn resize(&mut self, len: usize, additional: usize) -> Result<(), AllocError> {
143 self.inner.resize(len, additional)
144 }
145
146 #[inline]
147 fn try_merge<B>(&mut self, this_len: usize, other: B, other_len: usize) -> Result<(), B>
148 where
149 B: Alloc<T>,
150 {
151 self.inner.try_merge(this_len, other, other_len)
152 }
153}