swarm_pool
An object pooling system for Rust, optimized for perfomance.
Change log
- version 0.1.7:
- Removed
Copydependencies, PoolObjects do no longer rely on the implementation of theCopytrait. - Implemented the
Clone&Defaulttraits forSpawn. Spawn.clone() behaves in the same way as Spawn.mirror() does. The These where implemented so that PoolObjects can hold Spawn reference objects as field properties. Default on Spawn should not be used and is only there to make the above possible.
- Removed
- version 0.1.4:
- Added iterators
find(),for_while(),for_each(),for_all()&enumerate()toSwarmComtrol, these makes it possible to iterate over all spawns inside the update() loop.
- Added iterators
Using the swarm pool
The pooling system manages object instances of a cutom type, and provides update loops to iterate over them.
In order to create a new swarm pool, you need to define what your pool object and swarm properties types
are going to look like. Your pool object must at leas implement the Default, Copy and Clone traits
from the standard library. The swarm properties, on the other hand, does not depend on any traits.
Basic swarm setup example
extern crate swarm_pool;
use Swarm;
// create an object you want to pool
// create properties you want to share with pooled objects
;
The swarm is now ready to be used. First of all we need to Spawn new pool instances. In reality all objects in the pool are allready created and are waiting to be used. This means that all objects ( from 0 up to, but not including, the maximum capacity) can be accessed through the fetch() methode. The difference between spawned and non-spawned pool objects is that spawned object are included in all of the Swarm pools iterator methodes and non-spawned object are not.
Spawning and looping
let mut swarm = new;
let spawn1 = swarm.spawn.unwrap;
let spawn2 = swarm.spawn.unwrap;
assert_eq!;
assert_eq!;
swarm.for_each;
assert_eq!;
assert_eq!;
The real power of this library is not just looping through a few object instances, it is controlling and cross referencing them.
There are 2 powerful methodes that can be used to do so: Swarm.for_all() and Swarm.update().
Both have their advantages and disadvantages, for_all loop is fast (equal to a standard vec for loop) but cannot spawn nor kill
pool objects, update is easy to use, gives full control, but is slow (less than half the speed).
Cross referencing using for_all & update
// change properties to contain references to our spawned pool objects
let properties = MySwarmProperties ;
let mut swarm = new;
let s_john = swarm.spawn.unwrap;
let s_cristy = swarm.spawn.unwrap;
swarm.properties.john = Some;
swarm.properties.cristy = Some;
swarm.fetch.name = "John";
swarm.fetch.name = "Cristy";
// using the for_all methode
swarm.for_all;
assert_eq!;
assert_eq!;
// using the update methode
swarm.update;
assert_eq!;
assert_eq!;
There are many more functionalities included in the Swarm and SwarmControl types. The documentation on the examples above or other functionalities this library provides are more in depth and should be read, for writing them out was a lot of work ;)