Struct 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>

Source

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?
examples/simple.rs (line 18)
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}
Source

pub fn take(&self) -> DynamicPoolItem<T>

takes an item from the pool, creating one if none are available.

Examples found in repository?
examples/simple.rs (line 23)
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}
Source

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?
examples/simple.rs (line 42)
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}
Source

pub fn available(&self) -> usize

returns the number of free objects in the pool.

Examples found in repository?
examples/simple.rs (line 20)
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}
Source

pub fn used(&self) -> usize

returns the number of objects currently in use. does not include objects that have been detached.

Source

pub fn capacity(&self) -> usize

Trait Implementations§

Source§

impl<T: DynamicReset> Clone for DynamicPool<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + DynamicReset> Debug for DynamicPool<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for DynamicPool<T>

§

impl<T> !RefUnwindSafe for DynamicPool<T>

§

impl<T> Send for DynamicPool<T>
where T: Send,

§

impl<T> Sync for DynamicPool<T>
where T: Send,

§

impl<T> Unpin for DynamicPool<T>

§

impl<T> !UnwindSafe for DynamicPool<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.