Skip to content

Commit c78b76b

Browse files
committed
Provide better size_hint for QueryIter (#1697)
This PR overrides the default size_hint for QueryIter. This is mainly done to provide inline documentation of Issue #1686.
1 parent dd4a196 commit c78b76b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

crates/bevy_ecs/src/query/iter.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ where
123123
}
124124
}
125125
}
126+
127+
// NOTE: For unfiltered Queries this should actually return a exact size hint,
128+
// to fulfil the ExactSizeIterator invariant, but this isn't practical without specialization.
129+
// For more information see Issue #1686.
130+
fn size_hint(&self) -> (usize, Option<usize>) {
131+
let max_size = self
132+
.query_state
133+
.matched_archetypes
134+
.ones()
135+
.map(|index| self.world.archetypes[ArchetypeId::new(index)].len())
136+
.sum();
137+
138+
(0, Some(max_size))
139+
}
126140
}
127141

128142
// NOTE: We can cheaply implement this for unfiltered Queries because we have:

0 commit comments

Comments
 (0)