Implement FusedIterator for eligible Iterator types (#4942)
# Objective Most of our `Iterator` impls satisfy the requirements of `std::iter::FusedIterator`, which has internal specialization that optimizes `Interator::fuse`. The std lib iterator combinators do have a few that rely on `fuse`, so this could optimize those use cases. I don't think we're using any of them in the engine itself, but beyond a light increase in compile time, it doesn't hurt to implement the trait. ## Solution Implement the trait for all eligible iterators in first party crates. Also add a missing `ExactSizeIterator` on an iterator that could use it.
This commit is contained in:
parent
012ae07dc8
commit
f2b545049c
@ -196,6 +196,7 @@ impl<'a> Iterator for ReserveEntitiesIterator<'a> {
|
||||
}
|
||||
|
||||
impl<'a> core::iter::ExactSizeIterator for ReserveEntitiesIterator<'a> {}
|
||||
impl<'a> core::iter::FusedIterator for ReserveEntitiesIterator<'a> {}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Entities {
|
||||
|
@ -5,7 +5,7 @@ use crate::{
|
||||
query::{Fetch, QueryState, WorldQuery},
|
||||
storage::{TableId, Tables},
|
||||
};
|
||||
use std::{borrow::Borrow, marker::PhantomData, mem::MaybeUninit};
|
||||
use std::{borrow::Borrow, iter::FusedIterator, marker::PhantomData, mem::MaybeUninit};
|
||||
|
||||
use super::{QueryFetch, QueryItem, ReadOnlyFetch};
|
||||
|
||||
@ -72,6 +72,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// This is correct as [`QueryIter`] always returns `None` once exhausted.
|
||||
impl<'w, 's, Q: WorldQuery, QF, F: WorldQuery> FusedIterator for QueryIter<'w, 's, Q, QF, F> where
|
||||
QF: Fetch<'w, State = Q::State>
|
||||
{
|
||||
}
|
||||
|
||||
/// An [`Iterator`] over query results of a [`Query`](crate::system::Query).
|
||||
///
|
||||
/// This struct is created by the [`Query::iter_many`](crate::system::Query::iter_many) method.
|
||||
@ -359,6 +365,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// This is correct as [`QueryCombinationIter`] always returns `None` once exhausted.
|
||||
impl<'w, 's, Q: WorldQuery, F: WorldQuery, const K: usize> FusedIterator
|
||||
for QueryCombinationIter<'w, 's, Q, F, K>
|
||||
where
|
||||
QueryFetch<'w, Q>: Clone + ReadOnlyFetch,
|
||||
QueryFetch<'w, F>: Clone + ReadOnlyFetch,
|
||||
{
|
||||
}
|
||||
|
||||
struct QueryIterationCursor<'w, 's, Q: WorldQuery, QF: Fetch<'w, State = Q::State>, F: WorldQuery> {
|
||||
table_id_iter: std::slice::Iter<'s, TableId>,
|
||||
archetype_id_iter: std::slice::Iter<'s, ArchetypeId>,
|
||||
|
@ -3,6 +3,7 @@ use crate::{
|
||||
entity::Entity,
|
||||
world::World,
|
||||
};
|
||||
use std::iter::FusedIterator;
|
||||
|
||||
pub struct SpawnBatchIter<'w, I>
|
||||
where
|
||||
@ -84,3 +85,10 @@ where
|
||||
self.inner.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, T> FusedIterator for SpawnBatchIter<'_, I>
|
||||
where
|
||||
I: FusedIterator<Item = T>,
|
||||
T: Bundle,
|
||||
{
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::{serde::Serializable, Reflect, ReflectMut, ReflectRef};
|
||||
use std::fmt::Debug;
|
||||
use std::{
|
||||
any::Any,
|
||||
fmt::Debug,
|
||||
hash::{Hash, Hasher},
|
||||
};
|
||||
|
||||
|
@ -14,7 +14,7 @@ use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem};
|
||||
use bevy_math::*;
|
||||
use bevy_reflect::TypeUuid;
|
||||
use bevy_utils::Hashed;
|
||||
use std::{collections::BTreeMap, hash::Hash};
|
||||
use std::{collections::BTreeMap, hash::Hash, iter::FusedIterator};
|
||||
use thiserror::Error;
|
||||
use wgpu::{
|
||||
util::BufferInitDescriptor, BufferUsages, IndexFormat, VertexAttribute, VertexFormat,
|
||||
@ -749,8 +749,18 @@ impl Iterator for IndicesIter<'_> {
|
||||
IndicesIter::U32(iter) => iter.next().map(|val| *val as usize),
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
match self {
|
||||
IndicesIter::U16(iter) => iter.size_hint(),
|
||||
IndicesIter::U32(iter) => iter.size_hint(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ExactSizeIterator for IndicesIter<'a> {}
|
||||
impl<'a> FusedIterator for IndicesIter<'a> {}
|
||||
|
||||
impl From<&Indices> for IndexFormat {
|
||||
fn from(indices: &Indices) -> Self {
|
||||
match indices {
|
||||
|
@ -13,7 +13,7 @@ use bevy_asset::{AssetEvent, Assets, Handle};
|
||||
use bevy_ecs::event::EventReader;
|
||||
use bevy_ecs::system::{Res, ResMut};
|
||||
use bevy_utils::{default, tracing::error, Entry, HashMap, HashSet};
|
||||
use std::{hash::Hash, mem, ops::Deref, sync::Arc};
|
||||
use std::{hash::Hash, iter::FusedIterator, mem, ops::Deref, sync::Arc};
|
||||
use thiserror::Error;
|
||||
use wgpu::{PipelineLayoutDescriptor, ShaderModule, VertexBufferLayout as RawVertexBufferLayout};
|
||||
|
||||
@ -672,3 +672,5 @@ impl<'a> Iterator for ErrorSources<'a> {
|
||||
current
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FusedIterator for ErrorSources<'a> {}
|
||||
|
Loading…
Reference in New Issue
Block a user