pub struct DynamicPool<T: DynamicReset> { /* private fields */ }Expand description
a lock-free, thread-safe, dynamically-sized object pool.
this pool begins with an initial capacity and will continue creating new objects on request when none are available. pooled objects are returned to the pool on destruction (with an extra provision to optionally “reset” the state of an object for re-use).
if, during an attempted return, a pool already has maximum_capacity objects in the pool, the pool will throw away
that object.
Implementations§
Source§impl<T: DynamicReset> DynamicPool<T>
impl<T: DynamicReset> DynamicPool<T>
Sourcepub fn new<F: Fn() -> T + Sync + Send + 'static>(
initial_capacity: usize,
maximum_capacity: usize,
create: F,
) -> DynamicPool<T>
pub fn new<F: Fn() -> T + Sync + Send + 'static>( initial_capacity: usize, maximum_capacity: usize, create: F, ) -> DynamicPool<T>
creates a new DynamicPool<T>. this pool will create initial_capacity objects, and retain up to
maximum_capacity objects.
§panics.
panics if initial_capacity > maximum_capacity.
Examples found in repository?
16fn main() {
17 // Creates a new pool that will hold at most 10 items, starting with 1 item by default.
18 let pool = DynamicPool::new(1, 10, Person::default);
19 // Assert we have one item in the pool.
20 assert_eq!(pool.available(), 1);
21
22 // Take an item from the pool.
23 let mut person = pool.take();
24 person.name = "jake".into();
25 person.age = 99;
26
27 // Assert the pool is empty since we took the person above.
28 assert_eq!(pool.available(), 0);
29 // Dropping returns the item to the pool.
30 drop(person);
31 // We now have stuff available in the pool to take.
32 assert_eq!(pool.available(), 1);
33
34 // Take person from the pool again, it should be reset.
35 let person = pool.take();
36 assert_eq!(person.name, "");
37 assert_eq!(person.age, 0);
38
39 // Nothing is in the queue.
40 assert_eq!(pool.available(), 0);
41 // try_take returns an Option. Since the pool is empty, nothing will be created.
42 assert!(pool.try_take().is_none());
43 // Dropping again returns the person to the pool.
44 drop(person);
45 // We have stuff in the pool now!
46 assert_eq!(pool.available(), 1);
47
48 // try_take would succeed here!
49 let person = pool.try_take().unwrap();
50
51 // We can also then detach the `person` from the pool, meaning it won't get
52 // recycled.
53 let person = person.detach();
54 // We can then drop that person, and see that it's not returned to the pool.
55 drop(person);
56 assert_eq!(pool.available(), 0);
57}Sourcepub fn take(&self) -> DynamicPoolItem<T>
pub fn take(&self) -> DynamicPoolItem<T>
takes an item from the pool, creating one if none are available.
Examples found in repository?
16fn main() {
17 // Creates a new pool that will hold at most 10 items, starting with 1 item by default.
18 let pool = DynamicPool::new(1, 10, Person::default);
19 // Assert we have one item in the pool.
20 assert_eq!(pool.available(), 1);
21
22 // Take an item from the pool.
23 let mut person = pool.take();
24 person.name = "jake".into();
25 person.age = 99;
26
27 // Assert the pool is empty since we took the person above.
28 assert_eq!(pool.available(), 0);
29 // Dropping returns the item to the pool.
30 drop(person);
31 // We now have stuff available in the pool to take.
32 assert_eq!(pool.available(), 1);
33
34 // Take person from the pool again, it should be reset.
35 let person = pool.take();
36 assert_eq!(person.name, "");
37 assert_eq!(person.age, 0);
38
39 // Nothing is in the queue.
40 assert_eq!(pool.available(), 0);
41 // try_take returns an Option. Since the pool is empty, nothing will be created.
42 assert!(pool.try_take().is_none());
43 // Dropping again returns the person to the pool.
44 drop(person);
45 // We have stuff in the pool now!
46 assert_eq!(pool.available(), 1);
47
48 // try_take would succeed here!
49 let person = pool.try_take().unwrap();
50
51 // We can also then detach the `person` from the pool, meaning it won't get
52 // recycled.
53 let person = person.detach();
54 // We can then drop that person, and see that it's not returned to the pool.
55 drop(person);
56 assert_eq!(pool.available(), 0);
57}Sourcepub fn try_take(&self) -> Option<DynamicPoolItem<T>>
pub fn try_take(&self) -> Option<DynamicPoolItem<T>>
attempts to take an item from the pool, returning none if none is available. will never allocate.
Examples found in repository?
16fn main() {
17 // Creates a new pool that will hold at most 10 items, starting with 1 item by default.
18 let pool = DynamicPool::new(1, 10, Person::default);
19 // Assert we have one item in the pool.
20 assert_eq!(pool.available(), 1);
21
22 // Take an item from the pool.
23 let mut person = pool.take();
24 person.name = "jake".into();
25 person.age = 99;
26
27 // Assert the pool is empty since we took the person above.
28 assert_eq!(pool.available(), 0);
29 // Dropping returns the item to the pool.
30 drop(person);
31 // We now have stuff available in the pool to take.
32 assert_eq!(pool.available(), 1);
33
34 // Take person from the pool again, it should be reset.
35 let person = pool.take();
36 assert_eq!(person.name, "");
37 assert_eq!(person.age, 0);
38
39 // Nothing is in the queue.
40 assert_eq!(pool.available(), 0);
41 // try_take returns an Option. Since the pool is empty, nothing will be created.
42 assert!(pool.try_take().is_none());
43 // Dropping again returns the person to the pool.
44 drop(person);
45 // We have stuff in the pool now!
46 assert_eq!(pool.available(), 1);
47
48 // try_take would succeed here!
49 let person = pool.try_take().unwrap();
50
51 // We can also then detach the `person` from the pool, meaning it won't get
52 // recycled.
53 let person = person.detach();
54 // We can then drop that person, and see that it's not returned to the pool.
55 drop(person);
56 assert_eq!(pool.available(), 0);
57}Sourcepub fn available(&self) -> usize
pub fn available(&self) -> usize
returns the number of free objects in the pool.
Examples found in repository?
16fn main() {
17 // Creates a new pool that will hold at most 10 items, starting with 1 item by default.
18 let pool = DynamicPool::new(1, 10, Person::default);
19 // Assert we have one item in the pool.
20 assert_eq!(pool.available(), 1);
21
22 // Take an item from the pool.
23 let mut person = pool.take();
24 person.name = "jake".into();
25 person.age = 99;
26
27 // Assert the pool is empty since we took the person above.
28 assert_eq!(pool.available(), 0);
29 // Dropping returns the item to the pool.
30 drop(person);
31 // We now have stuff available in the pool to take.
32 assert_eq!(pool.available(), 1);
33
34 // Take person from the pool again, it should be reset.
35 let person = pool.take();
36 assert_eq!(person.name, "");
37 assert_eq!(person.age, 0);
38
39 // Nothing is in the queue.
40 assert_eq!(pool.available(), 0);
41 // try_take returns an Option. Since the pool is empty, nothing will be created.
42 assert!(pool.try_take().is_none());
43 // Dropping again returns the person to the pool.
44 drop(person);
45 // We have stuff in the pool now!
46 assert_eq!(pool.available(), 1);
47
48 // try_take would succeed here!
49 let person = pool.try_take().unwrap();
50
51 // We can also then detach the `person` from the pool, meaning it won't get
52 // recycled.
53 let person = person.detach();
54 // We can then drop that person, and see that it's not returned to the pool.
55 drop(person);
56 assert_eq!(pool.available(), 0);
57}Sourcepub fn used(&self) -> usize
pub fn used(&self) -> usize
returns the number of objects currently in use. does not include objects that have been detached.