Skip to content

Use <[T]>::as_chunks{_mut} for exact size chunks#23755

Open
DavidCrossman wants to merge 3 commits intobevyengine:mainfrom
DavidCrossman:use_as_chunks
Open

Use <[T]>::as_chunks{_mut} for exact size chunks#23755
DavidCrossman wants to merge 3 commits intobevyengine:mainfrom
DavidCrossman:use_as_chunks

Conversation

@DavidCrossman
Copy link
Copy Markdown
Contributor

Objective

  • Reduce unwraps and bounds checks.

Solution

  • Replace usages of <[T]>::chunks{_mut} and <[T]>::chunks_exact{_mut} where the size is exact with <[T]>::as_chunks{_mut}.

Testing

  • Ran cargo run -p ci -- compile

@kfc35 kfc35 added C-Code-Quality A section of code that is hard to understand or change S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Apr 11, 2026
Comment thread crates/bevy_mesh/src/mesh.rs Outdated
Comment on lines +1609 to +1615
self.try_indices()?
.iter()
.collect::<Vec<usize>>()
.chunks_exact(3)
.for_each(|face| per_triangle([face[0], face[1], face[2]], positions, &mut normals));
.as_chunks()
.0
.iter()
.for_each(|&faces| per_triangle(faces, positions, &mut normals));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collect::<Vec<usize>>() can be removed.

Suggested change
self.try_indices()?
.iter()
.collect::<Vec<usize>>()
.chunks_exact(3)
.for_each(|face| per_triangle([face[0], face[1], face[2]], positions, &mut normals));
.as_chunks()
.0
.iter()
.for_each(|&faces| per_triangle(faces, positions, &mut normals));
match self.try_indices()? {
Indices::U16(vec) => vec.as_chunks().0.iter().for_each(|&chunk| {
per_triangle(chunk.map(|i| i as usize), positions, &mut normals)
}),
Indices::U32(vec) => vec.as_chunks().0.iter().for_each(|&chunk| {
per_triangle(chunk.map(|i| i as usize), positions, &mut normals)
}),
}

Comment on lines 2219 to 2230
let iterator = match indices {
Indices::U16(vec) => FourIterators::First(
vec.as_slice()
.chunks_exact(3)
.flat_map(move |indices| indices_to_triangle(vertices, indices)),
),
Indices::U32(vec) => FourIterators::Second(
vec.as_slice()
.chunks_exact(3)
.flat_map(move |indices| indices_to_triangle(vertices, indices)),
),
};
Copy link
Copy Markdown
Member

@beicause beicause Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as_slice().chunks_exact(3) can be replaced with as_chunks::<3>().0

Co-authored-by: Luo Zhihao <luo_zhihao@outlook.com>
@beicause beicause added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Code-Quality A section of code that is hard to understand or change D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants