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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::scalar::{OpaqueScalar, ScalarAllocator};
mod ext;
mod flag;
mod sequence;
pub use flag::Flag;
pub use sequence::Sequence;
#[doc(hidden)]
pub mod export {
#[cfg(feature = "std")]
pub use super::ext::LocalKey;
}
#[doc(hidden)]
#[macro_export]
macro_rules! __global_pool {
($name:ident($imp:ty)) => {
impl $crate::Init for $name {
const INIT: Self = Self;
}
const _: () = {
static __IMP_POOL: $imp = $crate::Init::INIT;
impl<A: $crate::scalar::ScalarAllocator> $crate::pool::PoolMut<A> for $name
where
$imp: $crate::pool::Pool<A>,
{
fn insert_mut(
&mut self,
scalar: $crate::scalar::OpaqueScalar<A>,
) -> Option<$crate::scalar::OpaqueScalar<A>> {
$crate::pool::Pool::insert(&__IMP_POOL, scalar)
}
fn remove_mut(&mut self) -> Option<$crate::scalar::OpaqueScalar<A>> {
$crate::pool::Pool::remove(&__IMP_POOL)
}
}
impl<A: $crate::scalar::ScalarAllocator> $crate::pool::Pool<A> for $name
where
$imp: $crate::pool::Pool<A>,
{
fn insert(&self, scalar: $crate::scalar::OpaqueScalar<A>) -> Option<$crate::scalar::OpaqueScalar<A>> {
$crate::pool::Pool::insert(&__IMP_POOL, scalar)
}
fn remove(&self) -> Option<$crate::scalar::OpaqueScalar<A>> { $crate::pool::Pool::remove(&__IMP_POOL) }
}
};
};
(thread_local $name:ident($imp:ty)) => {
impl $crate::Init for $name {
const INIT: Self = Self;
}
const _: () = {
$crate::export::thread_local! {
static __IMP_POOL: $imp = $crate::export::Default::default();
}
impl<A: $crate::scalar::ScalarAllocator> $crate::pool::PoolMut<A> for $name
where
$imp: $crate::pool::Pool<A>,
{
fn insert_mut(
&mut self,
scalar: $crate::scalar::OpaqueScalar<A>,
) -> Option<$crate::scalar::OpaqueScalar<A>> {
$crate::pool::Pool::insert(&$crate::pool::export::LocalKey(&__IMP_POOL), scalar)
}
fn remove_mut(&mut self) -> Option<$crate::scalar::OpaqueScalar<A>> {
$crate::pool::Pool::remove(&$crate::pool::export::LocalKey(&__IMP_POOL))
}
}
impl<A: $crate::scalar::ScalarAllocator> $crate::pool::Pool<A> for $name
where
$imp: $crate::pool::Pool<A>,
{
fn insert(&self, scalar: $crate::scalar::OpaqueScalar<A>) -> Option<$crate::scalar::OpaqueScalar<A>> {
$crate::pool::Pool::insert(&$crate::pool::export::LocalKey(&__IMP_POOL), scalar)
}
fn remove(&self) -> Option<$crate::scalar::OpaqueScalar<A>> {
$crate::pool::Pool::remove(&$crate::pool::export::LocalKey(&__IMP_POOL))
}
}
};
};
}
#[macro_export]
macro_rules! global_pool {
(
$(#[$meta:meta])*
$v:vis struct $name:ident($imp:ty);
) => {
$(#[$meta])*
$v struct $name;
$crate::__global_pool!{$name($imp)}
};
(
$(#[$meta:meta])*
$v:vis thread_local struct $name:ident($imp:ty);
) => {
$(#[$meta])*
$v struct $name;
$crate::__global_pool!{thread_local $name($imp)}
};
}
#[cfg(feature = "alloc")]
cfg_if::cfg_if! {
if #[cfg(feature = "parking_lot")] {
use parking_lot::Mutex;
use std::vec::Vec;
use std::collections::VecDeque;
pub struct SyncStackPool<T: ScalarAllocator>(Mutex<Vec<crate::scalar::OpaqueScalar<T>>>);
pub struct SyncQueuePool<T: ScalarAllocator>(once_cell::sync::Lazy<Mutex<VecDeque<crate::scalar::OpaqueScalar<T>>>>);
} else if #[cfg(feature = "std")] {
use std::sync::Mutex;
use std::collections::VecDeque;
pub struct SyncStackPool<T: ScalarAllocator>(once_cell::sync::Lazy<Mutex<Vec<crate::scalar::OpaqueScalar<T>>>>);
pub struct SyncQueuePool<T: ScalarAllocator>(once_cell::sync::Lazy<Mutex<VecDeque<crate::scalar::OpaqueScalar<T>>>>);
}
}
pub trait PoolMut<A: ScalarAllocator> {
fn insert_mut(&mut self, scalar: OpaqueScalar<A>) -> Option<OpaqueScalar<A>>;
fn remove_mut(&mut self) -> Option<OpaqueScalar<A>>;
}
pub trait Pool<A: ScalarAllocator>: PoolMut<A> {
fn insert(&self, scalar: OpaqueScalar<A>) -> Option<OpaqueScalar<A>>;
fn remove(&self) -> Option<OpaqueScalar<A>>;
}
impl crate::Init for () {
const INIT: Self = ();
}
impl<A: ScalarAllocator> PoolMut<A> for () {
fn insert_mut(&mut self, scalar: OpaqueScalar<A>) -> Option<OpaqueScalar<A>> { Some(scalar) }
fn remove_mut(&mut self) -> Option<OpaqueScalar<A>> { None }
}
impl<A: ScalarAllocator> Pool<A> for () {
fn insert(&self, scalar: OpaqueScalar<A>) -> Option<OpaqueScalar<A>> { Some(scalar) }
fn remove(&self) -> Option<OpaqueScalar<A>> { None }
}
impl<P: ?Sized + PoolMut<A>, A: ScalarAllocator> PoolMut<A> for &mut P {
fn insert_mut(&mut self, scalar: OpaqueScalar<A>) -> Option<OpaqueScalar<A>> { P::insert_mut(self, scalar) }
fn remove_mut(&mut self) -> Option<OpaqueScalar<A>> { P::remove_mut(self) }
}
impl<P: ?Sized + Pool<A>, A: ScalarAllocator> Pool<A> for &mut P {
fn insert(&self, scalar: OpaqueScalar<A>) -> Option<OpaqueScalar<A>> { P::insert(self, scalar) }
fn remove(&self) -> Option<OpaqueScalar<A>> { P::remove(self) }
}
impl<P: ?Sized + Pool<A>, A: ScalarAllocator> PoolMut<A> for &P {
fn insert_mut(&mut self, scalar: OpaqueScalar<A>) -> Option<OpaqueScalar<A>> { P::insert(self, scalar) }
fn remove_mut(&mut self) -> Option<OpaqueScalar<A>> { P::remove(self) }
}
impl<P: ?Sized + Pool<A>, A: ScalarAllocator> Pool<A> for &P {
fn insert(&self, scalar: OpaqueScalar<A>) -> Option<OpaqueScalar<A>> { P::insert(self, scalar) }
fn remove(&self) -> Option<OpaqueScalar<A>> { P::remove(self) }
}