Skip to content

Commit

Permalink
Choose more descriptive field names for ReserveEntitiesIterator (#1…
Browse files Browse the repository at this point in the history
…5168)

No hard feelings if you don't want to make this change. This is just
something I stumbled over in my very first read of the `bevy_ecs` crate.

# Objective

- the general goal here is to improve DX slightly
- make the code easier to read in general. The previous names make the
code harder to read, especially since they are so similar.

## Solution

- choose more specific names for the fields
- `index_iter` -> `freelist_indices` : "freelist" is a well established
term in the rest of the docs in this module, so we might want to reuse
it
- `index_range` -> `new_indices` : Nothing besides the doc comment
stated that these indices were actually new/fresh

## Testing

Note that the fields are private so that this is no breaking change.
They are also only used in this one module.
  • Loading branch information
RobWalt authored Sep 20, 2024
1 parent 4742f74 commit 5484d2d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions crates/bevy_ecs/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,26 +450,26 @@ pub struct ReserveEntitiesIterator<'a> {
meta: &'a [EntityMeta],

// Reserved indices formerly in the freelist to hand out.
index_iter: std::slice::Iter<'a, u32>,
freelist_indices: std::slice::Iter<'a, u32>,

// New Entity indices to hand out, outside the range of meta.len().
index_range: std::ops::Range<u32>,
new_indices: std::ops::Range<u32>,
}

impl<'a> Iterator for ReserveEntitiesIterator<'a> {
type Item = Entity;

fn next(&mut self) -> Option<Self::Item> {
self.index_iter
self.freelist_indices
.next()
.map(|&index| {
Entity::from_raw_and_generation(index, self.meta[index as usize].generation)
})
.or_else(|| self.index_range.next().map(Entity::from_raw))
.or_else(|| self.new_indices.next().map(Entity::from_raw))
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.index_iter.len() + self.index_range.len();
let len = self.freelist_indices.len() + self.new_indices.len();
(len, Some(len))
}
}
Expand Down Expand Up @@ -589,8 +589,8 @@ impl Entities {

ReserveEntitiesIterator {
meta: &self.meta[..],
index_iter: self.pending[freelist_range].iter(),
index_range: new_id_start..new_id_end,
freelist_indices: self.pending[freelist_range].iter(),
new_indices: new_id_start..new_id_end,
}
}

Expand Down

0 comments on commit 5484d2d

Please sign in to comment.