Operator forwarding is written out by hand, 366 lines of it #33

Closed
opened 2026-08-16 00:35:27 +00:00 by rosa · 1 comment
Owner

What to build

Money and MoneyBag between them spend around 366 lines forwarding operators:
the same fn add(self, rhs: &Money) -> Money { self + *rhs } and its
permutations, written out for every combination of owned and borrowed operand,
for Add, Sub, AddAssign, and SubAssign.

The crate already shows how to do this once. impl_scalar_mul! generates eleven
impls and carries a # Panics note through the expansion. Extend that approach
to a macro covering the borrowed-operand triple and the assigning variants, and
use it for both types.

Behaviour does not change: the same operand combinations compile, and the same
panic messages come out.

Acceptance criteria

  • One macro generates the borrowed-operand forwarding for Add, Sub, AddAssign, and SubAssign
  • Both Money and MoneyBag use it
  • The # Panics documentation still appears on the generated impls in cargo doc
  • Every operand combination that compiles today still compiles
  • The existing panic-message tests pass unchanged
  • mise run ci passes

Blocked by

None — can start immediately. Note that this touches the same regions of
lib.rs and bag.rs that the module split and the MoneyBag copying work also
touch; whichever lands second will need to rebase.

## What to build `Money` and `MoneyBag` between them spend around 366 lines forwarding operators: the same `fn add(self, rhs: &Money) -> Money { self + *rhs }` and its permutations, written out for every combination of owned and borrowed operand, for `Add`, `Sub`, `AddAssign`, and `SubAssign`. The crate already shows how to do this once. `impl_scalar_mul!` generates eleven impls and carries a `# Panics` note through the expansion. Extend that approach to a macro covering the borrowed-operand triple and the assigning variants, and use it for both types. Behaviour does not change: the same operand combinations compile, and the same panic messages come out. ## Acceptance criteria - [ ] One macro generates the borrowed-operand forwarding for `Add`, `Sub`, `AddAssign`, and `SubAssign` - [ ] Both `Money` and `MoneyBag` use it - [ ] The `# Panics` documentation still appears on the generated impls in `cargo doc` - [ ] Every operand combination that compiles today still compiles - [ ] The existing panic-message tests pass unchanged - [ ] `mise run ci` passes ## Blocked by None — can start immediately. Note that this touches the same regions of `lib.rs` and `bag.rs` that the module split and the `MoneyBag` copying work also touch; whichever lands second will need to rebase.
Author
Owner

I've decided I'd rather have these hand-written than make some nasty macro

I've decided I'd rather have these hand-written than make some nasty macro
rosa closed this issue 2026-08-21 21:05:57 +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#33
No description provided.