Skip to content

Commit

Permalink
MaybeIter inbetween
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri committed Dec 25, 2022
1 parent e71ada6 commit 9eac180
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions crates/bevy_ecs/src/removal_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use crate::{
self as bevy_ecs,
component::{Component, ComponentId, ComponentIdFor},
entity::Entity,
event::{Events, ManualEventReader},
event::{Events, ManualEventIterator, ManualEventReader},
prelude::Local,
storage::SparseSet,
};
use bevy_ecs_macros::SystemParam;

use std::{
fmt::Debug,
iter::Cloned,
marker::PhantomData,
ops::{Deref, DerefMut},
};
Expand Down Expand Up @@ -116,13 +117,26 @@ pub struct RemovedComponents<'w, 's, T: Component> {
event_sets: &'w RemovedComponentEvents,
}

pub enum MaybeIter<I: Iterator> {
Iter(I),
Empty,
}

impl<I: Iterator> Iterator for MaybeIter<I> {
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
match self {
MaybeIter::Iter(iter) => iter.next(),
MaybeIter::Empty => None,
}
}
}
impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
pub fn iter(&mut self) -> Box<dyn Iterator<Item = Entity> + '_> {
pub fn iter(&mut self) -> MaybeIter<Cloned<ManualEventIterator<'_, Entity>>> {
if let Some(events) = self.event_sets.get(**self.component_id) {
// TODO: Make this an actually name-able type so we don't need to box
Box::new(self.reader.iter(events).cloned())
MaybeIter::Iter(self.reader.iter(events).cloned())
} else {
Box::new(std::iter::empty())
MaybeIter::Empty
}
}
}
Expand All @@ -132,7 +146,7 @@ where
T: Component,
{
type Item = Entity;
type IntoIter = Box<dyn Iterator<Item = Entity> + 'a>;
type IntoIter = MaybeIter<Cloned<ManualEventIterator<'a, Entity>>>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
Expand Down

0 comments on commit 9eac180

Please sign in to comment.