pub struct ParticlePool<P> { /* private fields */ }Expand description
A bounded, unordered set of live particles (CORE-R28).
Backed by one Vec<P> allocated up front. The logical capacity is tracked
separately from the Vec’s so that shrinking never reallocates and a later grow
back to the original size reuses the buffer.
Implementations§
Source§impl<P> ParticlePool<P>
impl<P> ParticlePool<P>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
An empty pool that will hold at most capacity particles (CORE-R28).
Sourcepub fn spawn(&mut self, p: P) -> bool
pub fn spawn(&mut self, p: P) -> bool
Add p if there is room; otherwise drop it and return false (CORE-R28).
Rejecting instead of evicting keeps spawn cost O(1) and makes the particle budget a hard cap that emitters can observe.
Sourcepub fn retain(&mut self, f: impl FnMut(&mut P) -> bool)
pub fn retain(&mut self, f: impl FnMut(&mut P) -> bool)
Update every particle in place and drop those for which f returns false
(CORE-R28). Order is not preserved.
Swap-remove compaction: a dead particle is replaced by the last one, which is
then visited at the same index. That is one pass, no shifting of the tail, and
no allocation — cheaper than Vec::retain_mut when many particles die at once,
and ordering is meaningless for particles anyway.
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn iter(&self) -> Iter<'_, P> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, P> ⓘ
Sourcepub fn set_capacity(&mut self, capacity: usize)
pub fn set_capacity(&mut self, capacity: usize)
Change the particle budget, e.g. on resize (CORE-R28).
Shrinking truncates (which particles survive is unspecified) and keeps the buffer; growing reserves exactly the extra room — the only allocating operation.
Trait Implementations§
Source§impl<P: Clone> Clone for ParticlePool<P>
impl<P: Clone> Clone for ParticlePool<P>
Source§impl<P: Debug> Debug for ParticlePool<P>
impl<P: Debug> Debug for ParticlePool<P>
Source§impl<P> Default for ParticlePool<P>
An empty pool of capacity 0, the placeholder until a scene sizes it with
ParticlePool::set_capacity; allocates nothing.
impl<P> Default for ParticlePool<P>
An empty pool of capacity 0, the placeholder until a scene sizes it with
ParticlePool::set_capacity; allocates nothing.