Struct dynamic_pool::DynamicPool
source · 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 + 'static>(
initial_capacity: usize,
maximum_capacity: usize,
create: F,
) -> DynamicPool<T>
pub fn new<F: Fn() -> T + Sync + '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?
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
fn main() {
// Creates a new pool that will hold at most 10 items, starting with 1 item by default.
let pool = DynamicPool::new(1, 10, Person::default);
// Assert we have one item in the pool.
assert_eq!(pool.available(), 1);
// Take an item from the pool.
let mut person = pool.take();
person.name = "jake".into();
person.age = 99;
// Assert the pool is empty since we took the person above.
assert_eq!(pool.available(), 0);
// Dropping returns the item to the pool.
drop(person);
// We now have stuff available in the pool to take.
assert_eq!(pool.available(), 1);
// Take person from the pool again, it should be reset.
let person = pool.take();
assert_eq!(person.name, "");
assert_eq!(person.age, 0);
// Nothing is in the queue.
assert_eq!(pool.available(), 0);
// try_take returns an Option. Since the pool is empty, nothing will be created.
assert!(pool.try_take().is_none());
// Dropping again returns the person to the pool.
drop(person);
// We have stuff in the pool now!
assert_eq!(pool.available(), 1);
// try_take would succeed here!
let person = pool.try_take().unwrap();
// We can also then detach the `person` from the pool, meaning it won't get
// recycled.
let person = person.detach();
// We can then drop that person, and see that it's not returned to the pool.
drop(person);
assert_eq!(pool.available(), 0);
}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?
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
fn main() {
// Creates a new pool that will hold at most 10 items, starting with 1 item by default.
let pool = DynamicPool::new(1, 10, Person::default);
// Assert we have one item in the pool.
assert_eq!(pool.available(), 1);
// Take an item from the pool.
let mut person = pool.take();
person.name = "jake".into();
person.age = 99;
// Assert the pool is empty since we took the person above.
assert_eq!(pool.available(), 0);
// Dropping returns the item to the pool.
drop(person);
// We now have stuff available in the pool to take.
assert_eq!(pool.available(), 1);
// Take person from the pool again, it should be reset.
let person = pool.take();
assert_eq!(person.name, "");
assert_eq!(person.age, 0);
// Nothing is in the queue.
assert_eq!(pool.available(), 0);
// try_take returns an Option. Since the pool is empty, nothing will be created.
assert!(pool.try_take().is_none());
// Dropping again returns the person to the pool.
drop(person);
// We have stuff in the pool now!
assert_eq!(pool.available(), 1);
// try_take would succeed here!
let person = pool.try_take().unwrap();
// We can also then detach the `person` from the pool, meaning it won't get
// recycled.
let person = person.detach();
// We can then drop that person, and see that it's not returned to the pool.
drop(person);
assert_eq!(pool.available(), 0);
}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?
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
fn main() {
// Creates a new pool that will hold at most 10 items, starting with 1 item by default.
let pool = DynamicPool::new(1, 10, Person::default);
// Assert we have one item in the pool.
assert_eq!(pool.available(), 1);
// Take an item from the pool.
let mut person = pool.take();
person.name = "jake".into();
person.age = 99;
// Assert the pool is empty since we took the person above.
assert_eq!(pool.available(), 0);
// Dropping returns the item to the pool.
drop(person);
// We now have stuff available in the pool to take.
assert_eq!(pool.available(), 1);
// Take person from the pool again, it should be reset.
let person = pool.take();
assert_eq!(person.name, "");
assert_eq!(person.age, 0);
// Nothing is in the queue.
assert_eq!(pool.available(), 0);
// try_take returns an Option. Since the pool is empty, nothing will be created.
assert!(pool.try_take().is_none());
// Dropping again returns the person to the pool.
drop(person);
// We have stuff in the pool now!
assert_eq!(pool.available(), 1);
// try_take would succeed here!
let person = pool.try_take().unwrap();
// We can also then detach the `person` from the pool, meaning it won't get
// recycled.
let person = person.detach();
// We can then drop that person, and see that it's not returned to the pool.
drop(person);
assert_eq!(pool.available(), 0);
}sourcepub fn available(&self) -> usize
pub fn available(&self) -> usize
returns the number of free objects in the pool.
Examples found in repository?
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
fn main() {
// Creates a new pool that will hold at most 10 items, starting with 1 item by default.
let pool = DynamicPool::new(1, 10, Person::default);
// Assert we have one item in the pool.
assert_eq!(pool.available(), 1);
// Take an item from the pool.
let mut person = pool.take();
person.name = "jake".into();
person.age = 99;
// Assert the pool is empty since we took the person above.
assert_eq!(pool.available(), 0);
// Dropping returns the item to the pool.
drop(person);
// We now have stuff available in the pool to take.
assert_eq!(pool.available(), 1);
// Take person from the pool again, it should be reset.
let person = pool.take();
assert_eq!(person.name, "");
assert_eq!(person.age, 0);
// Nothing is in the queue.
assert_eq!(pool.available(), 0);
// try_take returns an Option. Since the pool is empty, nothing will be created.
assert!(pool.try_take().is_none());
// Dropping again returns the person to the pool.
drop(person);
// We have stuff in the pool now!
assert_eq!(pool.available(), 1);
// try_take would succeed here!
let person = pool.try_take().unwrap();
// We can also then detach the `person` from the pool, meaning it won't get
// recycled.
let person = person.detach();
// We can then drop that person, and see that it's not returned to the pool.
drop(person);
assert_eq!(pool.available(), 0);
}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.
pub fn capacity(&self) -> usize
Trait Implementations§
source§impl<T: DynamicReset> Clone for DynamicPool<T>
impl<T: DynamicReset> Clone for DynamicPool<T>
Auto Trait Implementations§
impl<T> Freeze for DynamicPool<T>
impl<T> !RefUnwindSafe for DynamicPool<T>
impl<T> !Send for DynamicPool<T>
impl<T> !Sync for DynamicPool<T>
impl<T> Unpin for DynamicPool<T>
impl<T> !UnwindSafe for DynamicPool<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)