Skip to content

Add iter(), iter_mut(), drain(), and clear() - #5

Merged
nixonyh merged 3 commits into
mainfrom
nixon/sparse-map-iter
Jun 5, 2026
Merged

Add iter(), iter_mut(), drain(), and clear()#5
nixonyh merged 3 commits into
mainfrom
nixon/sparse-map-iter

Conversation

@nixonyh

@nixonyh nixonyh commented Jun 5, 2026

Copy link
Copy Markdown
Member
  • Add iter() & iter_mut() to iterate through the entire map (unordered)
  • Add drain() to drain the entire map while iterating through the values.
  • Add clear() to clear all values and invalidate existing keys (via generation incrementation).
  • Bumped version to 0.3.0

@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR extends SparseMap with live-value iteration and bulk removal APIs. New public methods iter(), iter_mut(), drain(), and clear() are added alongside a Drain<'a, T> iterator type. The draining iterator removes and invalidates entries as it traverses, with generation-aware slot recycling. Tests verify iteration, generation-based key invalidation, slot reuse, and early-drop behavior. Version bumped to 0.3.0.

Changes

SparseMap iteration and bulk removal APIs

Layer / File(s) Summary
API contract and version update
Cargo.toml, src/lib.rs (231–277)
New public methods iter(), iter_mut(), drain(), and clear() declared on SparseMap<T>. Version incremented from 0.2.0 to 0.3.0.
Drain iterator implementation
src/lib.rs (279–312)
Drain<'a, T> struct and Iterator impl remove occupied slots on each next(), bumping generations and updating the free-slot list. Drop impl ensures cleanup if iteration exits early.
Tests for iteration and bulk removal
src/lib.rs (591–678)
Tests verify iter()/iter_mut() behavior, drain() yields all entries and enables slot reuse while preventing stale-key reuse via generation bumping, clear() empties and invalidates keys, and early-drop of the draining iterator completes cleanup.

🎯 3 (Moderate) | ⏱️ ~25 minutes

🐰 With iter() and drain() now flowing,
And clear() slots brightly glowing,
No values lost to generation's test,
The SparseMap now performs its best! 🗺️✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and clearly summarizes the main changes, listing the four new methods being added to the API.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The pull request description clearly describes the changeset, listing the four main additions: iter(), iter_mut(), drain(), and clear() methods, plus version bump.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nixonyh nixonyh changed the title Add iter, iter_mut, drain, and clear Add iter(), iter_mut(), drain(), and clear() Jun 5, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lib.rs`:
- Around line 594-596: Several test cases call map.insert(...) and ignore its
#[must_use] return (a Key), causing clippy failures; update each call to either
capture the returned Key into a variable prefixed with _ (e.g., let _key =
map.insert(...);) or use the Key in assertions so the value is consumed. Search
for direct calls to map.insert in the tests (examples around the repeated
patterns inserting 1,2,3) and replace the bare calls with bound results (or
assert on them) to satisfy the must_use requirement.
- Around line 252-275: The bulk-removal paths (clear and drain) currently only
bump generations for slots where slot.take().is_some(), missing slots that are
"temporarily empty" (reserved via take()/scope()) so outstanding Key::restore()
can still succeed; update clear and Drain::drop (the drain implementation) to
treat any non-empty placeholder (i.e., any slot that is not the canonical Empty
variant) as a live slot that must have its generation bumped and be reclaimed:
iterate buffer entries and for each slot that is not Empty (including
reserved/placeholder states created by take()/scope()), increment
self.generations[index] (wrapping_add(1)), ensure the slot is set to Empty and
add its index to empty_slots so the map is truly emptied and Keys are
invalidated; apply the same logic to the drain() implementation and the
symmetric code block noted around the other range (the second occurrence) so
restore()/scope() can no longer resurrect values after clear()/drain().
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9f136536-5c97-4f62-a234-692b495692ba

📥 Commits

Reviewing files that changed from the base of the PR and between 083d106 and 1d25729.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • Cargo.toml
  • src/lib.rs

Comment thread src/lib.rs Outdated
Comment thread src/lib.rs Outdated
@nixonyh
nixonyh merged commit 7ed644f into main Jun 5, 2026
8 checks passed
@nixonyh
nixonyh deleted the nixon/sparse-map-iter branch June 5, 2026 15:26
nixonyh added a commit that referenced this pull request Jun 5, 2026
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.

1 participant