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
use crossbeam_queue::ArrayQueue;
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Weak};
use crate::DynamicReset;
#[derive(Debug)]
pub struct DynamicPool<T: DynamicReset> {
data: Arc<PoolData<T>>,
}
impl<T: DynamicReset> DynamicPool<T> {
pub fn new<F: Fn() -> T + Sync + Send + 'static>(
initial_capacity: usize,
maximum_capacity: usize,
create: F,
) -> DynamicPool<T> {
assert![initial_capacity <= maximum_capacity];
let items = ArrayQueue::new(maximum_capacity);
for x in (0..initial_capacity).map(|_| create()) {
items
.push(x)
.expect("invariant: items.len() always less than initial_capacity.");
}
let data = PoolData {
items,
create: Box::new(create),
};
let data = Arc::new(data);
DynamicPool { data }
}
pub fn take(&self) -> DynamicPoolItem<T> {
let object = self
.data
.items
.pop()
.unwrap_or_else(|_| (self.data.create)());
DynamicPoolItem {
data: Arc::downgrade(&self.data),
object: Some(object),
}
}
pub fn try_take(&self) -> Option<DynamicPoolItem<T>> {
let object = self.data.items.pop().ok()?;
let data = Arc::downgrade(&self.data);
Some(DynamicPoolItem {
data,
object: Some(object),
})
}
#[inline]
pub fn available(&self) -> usize {
self.data.items.len()
}
#[inline]
pub fn used(&self) -> usize {
Arc::weak_count(&self.data)
}
#[inline]
pub fn capacity(&self) -> usize {
self.data.items.capacity()
}
}
impl<T: DynamicReset> Clone for DynamicPool<T> {
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
}
}
}
struct PoolData<T> {
items: ArrayQueue<T>,
create: Box<dyn Fn() -> T + Sync + Send + 'static>,
}
impl<T: DynamicReset + Debug> Debug for PoolData<T> {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), std::fmt::Error> {
formatter
.debug_struct("PoolData")
.field("items", &self.items)
.field("create", &"Box<dyn Fn() -> T>")
.finish()
}
}
#[derive(Debug)]
pub struct DynamicPoolItem<T: DynamicReset> {
data: Weak<PoolData<T>>,
object: Option<T>,
}
impl<T: DynamicReset> DynamicPoolItem<T> {
pub fn detach(mut self) -> T {
self.object
.take()
.expect("invariant: object is always `some`.")
}
}
impl<T: DynamicReset> AsRef<T> for DynamicPoolItem<T> {
fn as_ref(&self) -> &T {
self.object
.as_ref()
.expect("invariant: object is always `some`.")
}
}
impl<T: DynamicReset> Deref for DynamicPoolItem<T> {
type Target = T;
fn deref(&self) -> &T {
self.object
.as_ref()
.expect("invariant: object is always `some`.")
}
}
impl<T: DynamicReset> DerefMut for DynamicPoolItem<T> {
fn deref_mut(&mut self) -> &mut T {
self.object
.as_mut()
.expect("invariant: object is always `some`.")
}
}
impl<T: DynamicReset> Drop for DynamicPoolItem<T> {
fn drop(&mut self) {
if let Some(mut object) = self.object.take() {
object.reset();
if let Some(pool) = self.data.upgrade() {
pool.items.push(object).ok();
}
}
}
}