iter with slot-wrapper by youknowone · Pull Request #6488 · RustPython/RustPython
📝 Walkthrough
Walkthrough
This pull request refactors iteration support in RustPython by moving __iter__ and __next__ implementations from trait methods to the slot wrapper mechanism. It introduces Iter and IterNext variants to the SlotFunc enum, adds a generic macro for unified slot wrapper insertion, and removes redundant trait method implementations.
Changes
| Cohort / File(s) | Summary |
|---|---|
Slot wrapper extension for iteration crates/vm/src/builtins/descriptor.rs |
Added Iter(IterFunc) and IterNext(IterNextFunc) variants to SlotFunc enum. Updated Debug implementation with new variants. Extended SlotFunc::call method to handle iteration slots: validates no arguments are passed for __iter__ and __next__, invokes underlying functions, and converts IterNext results via to_pyresult(vm). Imported ToPyResult and type aliases for iteration function support. |
Class slot wrapper insertion crates/vm/src/class.rs |
Introduced add_slot_wrapper macro to generically create and insert slot wrappers for __init__, __repr__, __iter__, and __next__. Retained special handling for __hash__ to set None when hash is not implemented. Consolidates slot wrapper insertion logic into uniform pattern aligned with CPython's behavior. |
Trait implementation refactoring crates/vm/src/types/slot.rs |
Removed explicit __iter__ method from Iterable trait and __next__ method from IterNext trait. Removed default implementations delegating to slot_iter and slot_iternext. Added comments indicating these methods are now exposed via SlotFunc wrapper mechanism. Removed ToPyResult import, retaining only ToPyObject. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~22 minutes
Possibly related PRs
- __hash__ to slot_wrapper #6480: Modifies the same slot-wrapper infrastructure (SlotFunc enum and dispatch logic in descriptor.rs) with this PR extending it by adding Iter/IterNext variants to the existing mechanism.
- Introduce slot wrapper to __init__ #4884: Earlier work on the slot-wrapper machinery introducing PySlotWrapper and method-wrapper pattern that this PR builds upon with the generic add_slot_wrapper macro.
Suggested reviewers
- ShaharNaveh
Poem
🐰 Iteration flows through slots so neat,
No traits needed, macro's sweet!
iter wraps with next in tow,
Unified wrapper steals the show!
Hopping forward, code runs tight, ✨
Pre-merge checks and finishing touches
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title directly reflects the main change: adding iteration slot support (iter and next) via slot-wrapper mechanism across descriptor.rs, class.rs, and types/slot.rs. |
✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
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.
Comment @coderabbitai help to get the list of available commands and usage tips.