Struct dynamic_pool::DynamicPoolItem
source · pub struct DynamicPoolItem<T: DynamicReset> { /* private fields */ }Expand description
an object, checked out from a dynamic pool object.
Implementations§
source§impl<T: DynamicReset> DynamicPoolItem<T>
impl<T: DynamicReset> DynamicPoolItem<T>
sourcepub fn detach(self) -> T
pub fn detach(self) -> T
detaches this instance from the pool, returns T.
Examples found in repository?
examples/simple.rs (line 53)
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);
}Trait Implementations§
source§impl<T: DynamicReset> AsRef<T> for DynamicPoolItem<T>
impl<T: DynamicReset> AsRef<T> for DynamicPoolItem<T>
source§impl<T: Debug + DynamicReset> Debug for DynamicPoolItem<T>
impl<T: Debug + DynamicReset> Debug for DynamicPoolItem<T>
source§impl<T: DynamicReset> Deref for DynamicPoolItem<T>
impl<T: DynamicReset> Deref for DynamicPoolItem<T>
source§impl<T: DynamicReset> DerefMut for DynamicPoolItem<T>
impl<T: DynamicReset> DerefMut for DynamicPoolItem<T>
source§impl<T: DynamicReset> Drop for DynamicPoolItem<T>
impl<T: DynamicReset> Drop for DynamicPoolItem<T>
Auto Trait Implementations§
impl<T> Freeze for DynamicPoolItem<T>where
T: Freeze,
impl<T> !RefUnwindSafe for DynamicPoolItem<T>
impl<T> !Send for DynamicPoolItem<T>
impl<T> !Sync for DynamicPoolItem<T>
impl<T> Unpin for DynamicPoolItem<T>where
T: Unpin,
impl<T> !UnwindSafe for DynamicPoolItem<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
Mutably borrows from an owned value. Read more