Skip to content

Commit 12f71a8

Browse files
authored
don't overflow when relations are empty (#18891)
# Objective - Fixes #18890 ## Solution - Don't overflow when substracting, bound at 0 ## Testing - Reproducer from the issue now works
1 parent 18e1bf1 commit 12f71a8

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

crates/bevy_ecs/src/relationship/relationship_source_collection.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,15 @@ impl OrderedRelationshipSourceCollection for Vec<Entity> {
213213

214214
fn place_most_recent(&mut self, index: usize) {
215215
if let Some(entity) = self.pop() {
216-
let index = index.min(self.len() - 1);
216+
let index = index.min(self.len().saturating_sub(1));
217217
self.insert(index, entity);
218218
}
219219
}
220220

221221
fn place(&mut self, entity: Entity, index: usize) {
222222
if let Some(current) = <[Entity]>::iter(self).position(|e| *e == entity) {
223223
// The len is at least 1, so the subtraction is safe.
224-
let index = index.min(self.len() - 1);
224+
let index = index.min(self.len().saturating_sub(1));
225225
Vec::remove(self, current);
226226
self.insert(index, entity);
227227
};

0 commit comments

Comments
 (0)