|
43 | 43 | //! assert_eq!(vec2.len(), 0); |
44 | 44 | //! ``` |
45 | 45 |
|
| 46 | +use std::alloc::{Allocator, Global}; |
| 47 | + |
46 | 48 | use crate::id::{Id, bit_vec::MixedBitSet}; |
47 | 49 |
|
48 | 50 | /// Trait for defining how objects are recycled, created, and prepared in a pool. |
@@ -234,13 +236,13 @@ pub trait Recycler<T> { |
234 | 236 | /// // Pool now contains 2 recycled vectors ready for reuse |
235 | 237 | /// ``` |
236 | 238 | #[derive(Debug)] |
237 | | -pub struct Pool<T, R> { |
238 | | - free: Vec<T>, |
| 239 | +pub struct Pool<T, R, A: Allocator = Global> { |
| 240 | + free: Vec<T, A>, |
239 | 241 | recycler: R, |
240 | 242 | capacity: usize, |
241 | 243 | } |
242 | 244 |
|
243 | | -impl<T, R> Pool<T, R> { |
| 245 | +impl<T, R> Pool<T, R, Global> { |
244 | 246 | /// Creates a new pool with the specified capacity and a default recycler. |
245 | 247 | /// |
246 | 248 | /// The `capacity` parameter sets the maximum number of objects that will be |
@@ -278,16 +280,30 @@ impl<T, R> Pool<T, R> { |
278 | 280 | /// let mut pool = MixedBitSetPool::<VarId>::with_recycler(5, recycler); |
279 | 281 | /// ``` |
280 | 282 | pub fn with_recycler(capacity: usize, recycler: R) -> Self { |
| 283 | + Self::with_recycler_in(capacity, recycler, Global) |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +impl<T, R, A: Allocator> Pool<T, R, A> { |
| 288 | + pub fn new_in(capacity: usize, alloc: A) -> Self |
| 289 | + where |
| 290 | + R: Default, |
| 291 | + { |
| 292 | + Self::with_recycler_in(capacity, R::default(), alloc) |
| 293 | + } |
| 294 | + |
| 295 | + pub fn with_recycler_in(capacity: usize, recycler: R, alloc: A) -> Self { |
281 | 296 | Self { |
282 | | - free: Vec::with_capacity(capacity), |
| 297 | + free: Vec::with_capacity_in(capacity, alloc), |
283 | 298 | recycler, |
284 | 299 | capacity, |
285 | 300 | } |
286 | 301 | } |
287 | 302 | } |
288 | 303 |
|
289 | | -impl<T, R> Pool<T, R> |
| 304 | +impl<T, R, A> Pool<T, R, A> |
290 | 305 | where |
| 306 | + A: Allocator, |
291 | 307 | R: Recycler<T>, |
292 | 308 | { |
293 | 309 | /// Changes the pool's capacity, adjusting internal storage as needed. |
@@ -673,4 +689,4 @@ where |
673 | 689 | /// // Return to pool for reuse |
674 | 690 | /// pool.release(bitset); |
675 | 691 | /// ``` |
676 | | -pub type MixedBitSetPool<I> = Pool<MixedBitSet<I>, MixedBitSetRecycler>; |
| 692 | +pub type MixedBitSetPool<I, A = Global> = Pool<MixedBitSet<I>, MixedBitSetRecycler, A>; |
0 commit comments