#pool #object-pool #autoreturn

auto_pool

A simple pool that returns items automatically after it's dropped

7 unstable releases (3 breaking)

new 0.3.2 Mar 7, 2025
0.3.1 Feb 28, 2025
0.2.0 Jan 30, 2025
0.1.0 Nov 6, 2024
0.0.2 Nov 6, 2024

#266 in Configuration

Download history 8/week @ 2024-11-13 17/week @ 2024-11-20 1/week @ 2024-12-04 4/week @ 2024-12-11 139/week @ 2025-01-29 18/week @ 2025-02-05 128/week @ 2025-02-12 24/week @ 2025-02-19 169/week @ 2025-02-26

345 downloads per month

MIT/Apache

15KB
223 lines

autoreturn-pool

This pool automatically manages the return of objects.

The primary difference from competitors is the interface - implementation follows the RAII pattern, meaning the user must provide objects when creating the pool.

The Pool class has only three public methods:

  • Constructor with_config(config: Config, items): Creates a new pool with a custom configuration.
  • Constructor new(items): An alias for with_config that uses the default configuration.
  • take(&self): Extracts an object from the pool.

The configuration allows you to set the wait duration for the take method (default is Duration::MAX, which essentially means "Wait until you receive it").

Examples:


fn main() -> Result<(), autoreturn_pool::Error> {
    // basic usage
    let pool = autoreturn_pool::Pool::new([1, 2])?;
    let item = pool.take()?.unwrap();

    // with custom config
    let config = autoreturn_pool::Config {
        wait_duration: std::time::Duration::from_millis(5),
    };
    let pool = autoreturn_pool::Pool::with_config(config, [1, 2])?;
    let item = pool.take()?.unwrap();
}
// with custom object:
#[derive(Default)]
struct MyObject {
    value: i32,
}
fn main() -> Result<(), autoreturn_pool::Error> {
    let pool_objects = [
        MyObject::default(),
        MyObject::default()
    ];
    let pool = autoreturn_pool::Pool::new(pool_objects)?;
    let mut item = pool.take()?.unwrap();
    Ok(())
}

Dependencies

~2–11MB
~129K SLoC