The two iterator pairs are the same code twice #35

Closed
opened 2026-08-16 00:35:28 +00:00 by rosa · 0 comments
Owner

What to build

Balances and IntoBalances in the bag module, and Rates and IntoRates in
the exchange table module, are the same code written twice — around 167 lines.
Each is a newtype over a BTreeMap iterator with five trait impls
(Iterator with size_hint, DoubleEndedIterator, ExactSizeIterator,
FusedIterator), plus a pair of free functions that differ only in which
constructor they call.

Write one private macro that generates a borrowed and an owned iterator over a
BTreeMap, given the two type names, the key and value types, the item type,
and how to build an item from an entry. Use it in both modules.

The four public type names, their documentation, and their examples stay as they
are.

Acceptance criteria

  • One macro generates both iterator pairs
  • Balances, IntoBalances, Rates, and IntoRates keep their names and public documentation
  • All five trait impls are still present on each, including size_hint and ExactSizeIterator::len
  • Counting and reversing without walking forward still work, as the existing tests check
  • mise run ci passes

Blocked by

None — can start immediately.

## What to build `Balances` and `IntoBalances` in the bag module, and `Rates` and `IntoRates` in the exchange table module, are the same code written twice — around 167 lines. Each is a newtype over a `BTreeMap` iterator with five trait impls (`Iterator` with `size_hint`, `DoubleEndedIterator`, `ExactSizeIterator`, `FusedIterator`), plus a pair of free functions that differ only in which constructor they call. Write one private macro that generates a borrowed and an owned iterator over a `BTreeMap`, given the two type names, the key and value types, the item type, and how to build an item from an entry. Use it in both modules. The four public type names, their documentation, and their examples stay as they are. ## Acceptance criteria - [ ] One macro generates both iterator pairs - [ ] `Balances`, `IntoBalances`, `Rates`, and `IntoRates` keep their names and public documentation - [ ] All five trait impls are still present on each, including `size_hint` and `ExactSizeIterator::len` - [ ] Counting and reversing without walking forward still work, as the existing tests check - [ ] `mise run ci` passes ## Blocked by None — can start immediately.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rosa/lucre#35
No description provided.