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

Source

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>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

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

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: DynamicReset> Deref for DynamicPoolItem<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: DynamicReset> DerefMut for DynamicPoolItem<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T: DynamicReset> Drop for DynamicPoolItem<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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>
where T: Sync + Send,

§

impl<T> Unpin for DynamicPoolItem<T>
where T: Unpin,

§

impl<T> !UnwindSafe for DynamicPoolItem<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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.