bevy_ecs/world/
spawn_batch.rsuse crate::{
bundle::{Bundle, BundleSpawner},
entity::Entity,
world::World,
};
use std::iter::FusedIterator;
pub struct SpawnBatchIter<'w, I>
where
I: Iterator,
I::Item: Bundle,
{
inner: I,
spawner: BundleSpawner<'w>,
}
impl<'w, I> SpawnBatchIter<'w, I>
where
I: Iterator,
I::Item: Bundle,
{
#[inline]
pub(crate) fn new(world: &'w mut World, iter: I) -> Self {
world.flush();
let change_tick = world.change_tick();
let (lower, upper) = iter.size_hint();
let length = upper.unwrap_or(lower);
world.entities.reserve(length as u32);
let mut spawner = BundleSpawner::new::<I::Item>(world, change_tick);
spawner.reserve_storage(length);
Self {
inner: iter,
spawner,
}
}
}
impl<I> Drop for SpawnBatchIter<'_, I>
where
I: Iterator,
I::Item: Bundle,
{
fn drop(&mut self) {
for _ in &mut *self {}
unsafe { self.spawner.flush_commands() };
}
}
impl<I> Iterator for SpawnBatchIter<'_, I>
where
I: Iterator,
I::Item: Bundle,
{
type Item = Entity;
fn next(&mut self) -> Option<Entity> {
let bundle = self.inner.next()?;
unsafe { Some(self.spawner.spawn(bundle)) }
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<I, T> ExactSizeIterator for SpawnBatchIter<'_, I>
where
I: ExactSizeIterator<Item = T>,
T: Bundle,
{
fn len(&self) -> usize {
self.inner.len()
}
}
impl<I, T> FusedIterator for SpawnBatchIter<'_, I>
where
I: FusedIterator<Item = T>,
T: Bundle,
{
}