A currency pair is the board's key, but the board keys on a bare tuple #22

Closed
opened 2026-08-15 02:15:08 +00:00 by rosa · 0 comments
Owner

Exchange keys its board on BTreeMap<(Currency, Currency), Decimal>
(src/exchange/table.rs:62), an anonymous tuple that means exactly what Pair
means one file over — the currency priced and the currency it is priced in, in
that order.

Nothing names the tuple, so nothing stops the two currencies being transposed at
a call site: rate, set_rate, remove_rate, and the iterators all build or
destructure (base, quote) positionally, and a swap type-checks. Pair was
introduced with named fields for that reason and already carries the ordering
the table documents — "by the currency priced, then by the currency it is priced
in" is derived Ord on the pair.

Key the map on Pair instead. Pair gains Ord and PartialOrd, the public
signatures stay as they are — callers still pass two currencies — and the
conversion happens once at the boundary rather than at every entry.

Whether Pair should also appear in public signatures is a separate question,
and answering "no" here costs nothing later.

`Exchange` keys its board on `BTreeMap<(Currency, Currency), Decimal>` (`src/exchange/table.rs:62`), an anonymous tuple that means exactly what `Pair` means one file over — the currency priced and the currency it is priced in, in that order. Nothing names the tuple, so nothing stops the two currencies being transposed at a call site: `rate`, `set_rate`, `remove_rate`, and the iterators all build or destructure `(base, quote)` positionally, and a swap type-checks. `Pair` was introduced with named fields for that reason and already carries the ordering the table documents — "by the currency priced, then by the currency it is priced in" is derived `Ord` on the pair. Key the map on `Pair` instead. `Pair` gains `Ord` and `PartialOrd`, the public signatures stay as they are — callers still pass two currencies — and the conversion happens once at the boundary rather than at every entry. Whether `Pair` should also appear in public signatures is a separate question, and answering "no" here costs nothing later.
rosa closed this issue 2026-08-15 03:07:13 +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#22
No description provided.