Skip to main content

Module unbounded

Module unbounded 

Source
Expand description

Unbounded object pools.

An unbounded pool, on the other hand, allows you to put objects to the pool manually. You can use it like Go’s sync.Pool.

To configure a factory for creating objects when the pool is empty, like sync.Pool’s New, you can create the unbounded pool via Pool::new with an implementation of ManageObject.

§Examples

Read the following simple demos or more complex examples in the examples directory.

  1. Create an unbounded pool with NeverManageObject:
use fastpool::unbounded::Pool;
use fastpool::unbounded::PoolConfig;

let pool = Pool::<Vec<u8>>::never_manage(PoolConfig::default());

let result = pool.get().await;
assert_eq!(result.unwrap_err().to_string(), "unbounded pool is empty");

pool.extend_one(Vec::with_capacity(1024));
let o = pool.get().await.unwrap();
assert_eq!(o.capacity(), 1024);
drop(o);
let o = pool.get().await.unwrap();
assert_eq!(o.capacity(), 1024);
let result = pool.get().await;
assert_eq!(result.unwrap_err().to_string(), "unbounded pool is empty");
  1. Create an unbounded pool with a custom ManageObject (object factory):
use std::future::Future;

use fastpool::ManageObject;
use fastpool::ObjectStatus;
use fastpool::unbounded::Pool;
use fastpool::unbounded::PoolConfig;

struct Compute;
impl Compute {
    async fn do_work(&self) -> i32 {
        42
    }
}

struct Manager;
impl ManageObject for Manager {
    type Object = Compute;
    type Error = ();

    async fn create(&self) -> Result<Self::Object, Self::Error> {
        Ok(Compute)
    }

    async fn is_recyclable(
        &self,
        o: &mut Self::Object,
        status: &ObjectStatus,
    ) -> Result<(), Self::Error> {
        Ok(())
    }
}

let pool = Pool::new(PoolConfig::default(), Manager);
let o = pool.get().await.unwrap();
assert_eq!(o.do_work().await, 42);

Structs§

NeverManageObject
The default ManageObject implementation for unbounded pool.
Object
A wrapper of the actual pooled object.
Pool
Generic runtime-agnostic unbounded object pool.
PoolConfig
The configuration of Pool.
PoolIsEmpty
The error returned by NeverManageObject::create.
PoolStatus
The current pool status.