Skip to content

refactor: remove bump field from ChunkIter#319

Closed
overlookmotel wants to merge 1 commit intofitzgen:mainfrom
overlookmotel:chunk-iter
Closed

refactor: remove bump field from ChunkIter#319
overlookmotel wants to merge 1 commit intofitzgen:mainfrom
overlookmotel:chunk-iter

Conversation

@overlookmotel
Copy link
Copy Markdown
Contributor

Minor simplification.

It's unnecessary for ChunkIter to contain a PhantomData<&'a mut Bump> because it also contains a ChunkRawIter<'a, MIN_ALIGN> - and that contains PhantomData<&'a mut Bump>. This is sufficient to "hold" the lifetime for both iterators.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to simplify ChunkIter by removing its redundant bump: PhantomData<&'a mut Bump> field, relying on ChunkRawIter to carry the lifetime instead.

Changes:

  • Remove the bump: PhantomData<&'a mut Bump> field from ChunkIter
  • Simplify iter_allocated_chunks to construct ChunkIter with only its raw iterator

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -2388,7 +2385,6 @@ impl<const MIN_ALIGN: usize> Bump<MIN_ALIGN> {
#[derive(Debug)]
pub struct ChunkIter<'a, const MIN_ALIGN: usize = 1> {
raw: ChunkRawIter<'a, MIN_ALIGN>,
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Removing bump: PhantomData<&'a mut Bump<MIN_ALIGN>> from ChunkIter weakens the borrow the iterator holds to an immutable borrow via ChunkRawIter (ChunkRawIter currently uses PhantomData<&'a Bump<MIN_ALIGN>>). Since Bump has allocation APIs that take &self (interior mutability), callers can allocate while a safe ChunkIter is alive, which can invalidate the returned chunk slices and lead to UB. Keep the &'a mut Bump phantom in ChunkIter, or otherwise ensure the iterator type enforces exclusive access for the duration of iteration.

Suggested change
raw: ChunkRawIter<'a, MIN_ALIGN>,
raw: ChunkRawIter<'a, MIN_ALIGN>,
bump: PhantomData<&'a mut Bump<MIN_ALIGN>>,

Copilot uses AI. Check for mistakes.
Comment on lines 2149 to +2152
pub fn iter_allocated_chunks(&mut self) -> ChunkIter<'_, MIN_ALIGN> {
// Safety: Ensured by mutable borrow of `self`.
let raw = unsafe { self.iter_allocated_chunks_raw() };
ChunkIter {
raw,
bump: PhantomData,
}
ChunkIter { raw }
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

The safety comment here (“Ensured by mutable borrow of self.”) becomes incorrect if ChunkIter no longer carries a PhantomData<&'a mut Bump<MIN_ALIGN>>: the iterator only keeps an immutable borrow, and Bump exposes mutation via &self (e.g., alloc, try_alloc_layout, etc.). This means allocations can occur while the safe iterator is alive, violating the safety preconditions for producing &'a [MaybeUninit<u8>].

Copilot uses AI. Check for mistakes.
@overlookmotel
Copy link
Copy Markdown
Contributor Author

Copilot's critique sounds reasonable! I'm not actually sure he's right, but keeping the bump field is more explicit. Closing.

@overlookmotel overlookmotel deleted the chunk-iter branch April 2, 2026 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants