Expand description

Derivable Object Pool

This crate provides a trait that can be derived to implement an object pool for a type with a single line of code. Allowing the user to forget about the implementation details of the ObjectPool and focus on the important parts of their code

This crate has the following features compared to other object pool crates:

  • Derivable: The pool is simple to use and can be used with any type. Can be just derived using the #[derive(ObjectPool)] attribute macro.
  • Reusable: The user can use the ObjectPool::new function to create objects from the pool, which will reuse objects from the pool if possible. This items are wrapped in a Reusable struct, which will be returned to the pool when dropped.
  • Thread Safe: The pool is thread-safe (through the use of a Mutex) and can be used in a multi-threaded environment.
  • Simple: The user doesn’t need to create a pool for each type manually and can use the ObjectPool::new function to create objects from the pool.
  • Flexible: The user can configure the pool to use a custom generator function (see attributes in #[derive(ObjectPool)]) or just use the Default trait to create new objects.

Example

use derivable_object_pool::prelude::*;

#[derive(Default, ObjectPool)]
struct Test(i32);

fn main() {
    let mut obj = Test::new();
    obj.0 += 1;
    assert_eq!(obj.0, 1);
    drop(obj); // obj is returned to the pool
    assert_eq!(Test::pool().len(), 1);
    let mut obj = Test::new();
    assert_eq!(Test::pool().len(), 0);
    assert_eq!(obj.0, 1);
}

Modules

  • This is the prelude for the derivable-object-pool crate. It contains the main traits and structs that you will need to use the crate. It is recommended that you import this prelude at the top of your file.

Structs

  • A pool of objects that can be reused. This is useful for objects that are expensive to create, but are used frequently. This struct can be created using the Pool::new function. However, it is highly recommended that you use the ObjectPool trait instead, as it is much easier to use.
  • A wrapper for an object that will return the object to the pool when it is dropped. This is useful for objects that are expensive to create, but are used frequently. This struct can be created using the Pool::remove_reusable function. However, it is highly recommended that you use the ObjectPool::new function instead, as it will reuse objects from the pool if possible.

Traits

  • Allows for the creation of objects that can be reused. This is useful for objects that are expensive to create, but are used frequently. This trait can be derived using the #[derive(ObjectPool)] attribute macro (for more information, see the documentation for the ObjectPool trait)

Derive Macros

  • Derive macro for ObjectPool trait implementation. Optionally, you can specify a generator function for the pool. If not specified, the trait will try to use Default trait implementation.