A MoneyBag copies itself to answer questions about itself #32

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

What to build

MoneyBag::format_with says nothing is allocated until the result is displayed.
It clones the whole bag when it is called. The use<> in its return type is what
forces the copy; borrowing instead makes the sentence true.

The same copy happens three more times. covers, checked_add, and
checked_sub take impl Into<MoneyBag> and immediately convert, so passing a
borrowed bag deep-copies it just to read its balances. From<&MoneyBag> for MoneyBag exists only to satisfy that bound.

Replace the bound with a sealed private trait that hands back an iterator of
balances, implemented for Money, &Money, MoneyBag, and &MoneyBag. The
three methods keep the argument types they accept today and stop copying.
From<&MoneyBag> for MoneyBag goes away.

Acceptance criteria

  • format_with borrows the bag, and its documentation matches what it does
  • covers, checked_add, and checked_sub read a borrowed bag without copying it
  • All four argument forms — owned and borrowed Money, owned and borrowed MoneyBag — still work at every call site
  • From<&MoneyBag> for MoneyBag is gone
  • The trait is private and sealed, so it does not widen the public API
  • mise run ci passes

Blocked by

None — can start immediately.

## What to build `MoneyBag::format_with` says nothing is allocated until the result is displayed. It clones the whole bag when it is called. The `use<>` in its return type is what forces the copy; borrowing instead makes the sentence true. The same copy happens three more times. `covers`, `checked_add`, and `checked_sub` take `impl Into<MoneyBag>` and immediately convert, so passing a borrowed bag deep-copies it just to read its balances. `From<&MoneyBag> for MoneyBag` exists only to satisfy that bound. Replace the bound with a sealed private trait that hands back an iterator of balances, implemented for `Money`, `&Money`, `MoneyBag`, and `&MoneyBag`. The three methods keep the argument types they accept today and stop copying. `From<&MoneyBag> for MoneyBag` goes away. ## Acceptance criteria - [ ] `format_with` borrows the bag, and its documentation matches what it does - [ ] `covers`, `checked_add`, and `checked_sub` read a borrowed bag without copying it - [ ] All four argument forms — owned and borrowed `Money`, owned and borrowed `MoneyBag` — still work at every call site - [ ] `From<&MoneyBag> for MoneyBag` is gone - [ ] The trait is private and sealed, so it does not widen the public API - [ ] `mise run ci` passes ## Blocked by None — can start immediately.
rosa closed this issue 2026-08-16 19:42:30 +00:00
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#32
No description provided.