# base/objectpool (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/objectpool/

Object pool. (the original base/objectpool.js)
- Reuses objects created / freed every frame (bullets, effects and such) to reduce allocations. (the original was made to reduce GC pressure)
- Borrow with acquire() and return with release(). On return, reset_handler clears the state.
- The Rust version is a generic with the held type fixed as T. acquire takes an object out and hands it over (ownership moves), and release takes it back.
  The original's ignoring of release(null) was dropped because Rust has no null.
- clear() empties only the idle list as in the original, and leaves the number created so far (total_count) as is.

## ObjectCreateHandler (type)

Function that creates a new object.

```rust
pub type ObjectCreateHandler<T> = Box<dyn FnMut() -> T>
```

## ObjectResetHandler (type)

Function that clears the state on release. (receives the object as an argument)

```rust
pub type ObjectResetHandler<T> = Box<dyn FnMut(&mut T)>
```

## ObjectPool (struct)

Object pool. (lends out T objects, takes them back and reuses them)

Example: Pre-create bullets, then borrow and return them

```rust
use vanilla::ObjectPool;

struct Bullet {
	travel_distance: f64,
}

let mut object_pool = ObjectPool::new(
	Box::new(|| Bullet { travel_distance: 0.0 }),
	Some(Box::new(|bullet: &mut Bullet| {
		bullet.travel_distance = 0.0;
	})),
	2,
);
assert_eq!(object_pool.get_free_count(), 2);
let mut bullet = object_pool.acquire();
bullet.travel_distance = 120.0;
object_pool.release(bullet);
let reused_bullet = object_pool.acquire();
assert_eq!(reused_bullet.travel_distance, 0.0);
assert_eq!(object_pool.get_total_count(), 2);
```

- `pub fn new(create_handler: ObjectCreateHandler<T>, reset_handler: Option<ObjectResetHandler<T>>, preload_count: usize) -> ObjectPool<T>` (constructor, since 0.3.2): Constructor. (create_handler creates a new object, reset_handler clears the state on return, and preload_count objects are created in advance)
  - `create_handler` ObjectCreateHandler<T>: Function that creates a new object. (called when preloading, and when borrowing with nothing idle)
  - `reset_handler` Option<ObjectResetHandler<T>>: Function that clears the state on return. If None, the object is put back as is without clearing.
  - `preload_count` usize: Number of objects to create in advance at creation.
  - returns ObjectPool<T>: A pool holding preload_count idle objects. (the total count is also preload_count)
- `pub fn acquire(&mut self) -> T` (method, since 0.3.2): Borrows. (creates a new one if nothing is idle; the most recently returned goes out first)
  - returns T: The borrowed object. (ownership is handed over; return it with release when done)
- `pub fn release(&mut self, mut instance: T)` (method, since 0.3.2): Returns. (clears the state with reset_handler, then puts it in the idle list)
  - `instance` T: Object to return. (does not check whether it was borrowed from this pool, does not change the total count)
- `pub fn get_free_count(&self) -> usize` (method, since 0.3.2): Returns the number of idle objects.
  - returns usize: Number of objects in the idle list.
- `pub fn get_total_count(&self) -> usize` (method, since 0.3.2): Returns the total number of objects created so far.
  - returns usize: Number of objects created with create_handler. (does not decrease on clear)
- `pub fn clear(&mut self)` (method, since 0.3.2): Empties the pool. (empties only the idle list)
