use std::iter::Iterator;
use crate::{util::KeyType, Storage, Entity, World, EntityId};
#[allow(clippy::module_name_repetitions)]
pub struct PredIter<'a, 'b, S>
where
S : Storage<'a>,
S::Id: KeyType,
{
pub(in super) world: &'b World<'a, S>,
pub(in super) id: usize,
pub(in super) cur_idx: usize,
}
#[allow(clippy::module_name_repetitions)]
pub struct PredIterMut<'a, 'b, S>
where
S : Storage<'a>,
S::Id: KeyType,
{
pub(in super) world: &'b mut World<'a, S>,
pub(in super) id: usize,
pub(in super) cur_idx: usize,
}
impl<'a, 'b, S> Iterator for PredIter<'a, 'b, S>
where
S : Storage<'a>,
S::Id: KeyType,
{
type Item = &'b Entity<'a, S>;
#[allow(clippy::integer_arithmetic)] fn next(&mut self) -> Option< Self::Item >
{
while let Some(entity_id) = self.world.predicates
.get(&self.id)
.expect("Could not get predicate from id")
.ids
.get(self.cur_idx)
{
self.cur_idx += 1;
if let Some(entity) = self.world.entities.get( &entity_id.get() )
{
return Some( unsafe {
std::mem::transmute::<&'_ _, &'b _>(entity)
});
}
else {
entity_id.set( EntityId::null() );
}
}
None
}
}
impl<'a, 'b, S> Iterator for PredIterMut<'a, 'b, S>
where
S : Storage<'a>,
S::Id: KeyType,
{
type Item = &'b mut Entity<'a, S>;
#[allow(clippy::integer_arithmetic)] fn next(&mut self) -> Option< Self::Item >
{
while let Some(entity_id) = self.world.predicates
.get_mut(&self.id)
.expect("Could not get predicate from id")
.ids
.get_mut(self.cur_idx)
{
self.cur_idx += 1;
if let Some(entity) = self.world.entities.get_mut( &entity_id.get() )
{
return Some( unsafe {
std::mem::transmute::<&'_ mut _, &'b mut _>(entity)
});
}
else {
entity_id.set( EntityId::null() );
}
}
None
}
}