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)
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}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>where
T: Send,
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