[naga] Simplify iterator construction in type_expression_tandem. (#7070)

Rather than reversing two iterators and then zipping them, zip them
first and then reverse the result.

However, zipped iterators are only reversible if the inputs implement
`ExactSizeIterator`, so make `UniqueArena::iter` promise that as well.

For consistency, make `Arena::iter` also promise to return an
`ExactSizeIterator`.
This commit is contained in:
Jim Blandy
2025-02-06 17:41:55 -08:00
committed by GitHub
parent dad9d0b577
commit 9d7ec93d28
3 changed files with 5 additions and 4 deletions

View File

@@ -94,7 +94,7 @@ impl<T> Arena<T> {
/// Returns an iterator over the items stored in this arena, returning both
/// the item's handle and a reference to it.
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Handle<T>, &T)> {
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Handle<T>, &T)> + ExactSizeIterator {
self.data
.iter()
.enumerate()

View File

@@ -108,7 +108,7 @@ impl<T> Iterator for UniqueArenaDrain<'_, T> {
impl<T: Eq + hash::Hash> UniqueArena<T> {
/// Returns an iterator over the items stored in this arena, returning both
/// the item's handle and a reference to it.
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Handle<T>, &T)> {
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Handle<T>, &T)> + ExactSizeIterator {
self.set.iter().enumerate().map(|(i, v)| {
let index = unsafe { Index::new_unchecked(i as u32) };
(Handle::new(index), v)

View File

@@ -303,8 +303,9 @@ impl<'module> ModuleTracer<'module> {
// as used by the time we visit it is genuinely unused, and can be
// ignored.
let mut exprs = self.module.global_expressions.iter().rev().peekable();
for ((ty_handle, ty), dep) in self.module.types.iter().rev().zip(max_dep.iter().rev()) {
while let Some((expr_handle, expr)) = exprs.next_if(|&(h, _)| Some(h) > *dep) {
for ((ty_handle, ty), dep) in self.module.types.iter().zip(max_dep).rev() {
while let Some((expr_handle, expr)) = exprs.next_if(|&(h, _)| Some(h) > dep) {
if self.global_expressions_used.contains(expr_handle) {
self.as_const_expression().trace_expression(expr);
}