The Copy types take self by value and by reference #15

Closed
opened 2026-08-14 22:26:12 +00:00 by rosa · 0 comments
Owner

The Copy types now take their parameters by value (#10), but their
receivers are still split two ways, and Parser shows both in one type:

  • Parser::assume_currency(mut self, currency: Currency) -> Self — by value.
  • Parser::parse(&self, text: &str) -> Result<Money, ParseMoneyError> — by
    reference.

Format divides the same way: every builder method takes mut self, while
Money::format_with(&self, format: Format) reaches for one by reference.
ExchangeRate goes further and spells the same type both ways inside a single
signature:

pub fn cross_with(&self, other: Self) -> Result<Self, CrossRateError>

Money::checked_add(&self, other: Money) reads the same way. Having just
settled the argument side, the receiver is what is left of the question #10
asked: a reader still has no rule to carry from one call to the next.

Everything on Money, Currency, ExchangeRate, IsoNumericCode, and
Parser::parse is a candidate. One method cannot follow, and should stay as it
is: IsoAlphabeticCode::as_str hands back a &str borrowed from the code's own
bytes, so it needs a receiver that outlives the call.

Clippy has nothing to say here — trivially_copy_pass_by_ref leaves receivers
alone, and a pedantic run is clean — so this is a design choice rather than a
lint to satisfy.

Breaking for anyone holding a &Money and calling a method on it directly,
though auto-deref covers most call sites unchanged.

The `Copy` types now take their *parameters* by value (#10), but their *receivers* are still split two ways, and `Parser` shows both in one type: - `Parser::assume_currency(mut self, currency: Currency) -> Self` — by value. - `Parser::parse(&self, text: &str) -> Result<Money, ParseMoneyError>` — by reference. `Format` divides the same way: every builder method takes `mut self`, while `Money::format_with(&self, format: Format)` reaches for one by reference. `ExchangeRate` goes further and spells the same type both ways inside a single signature: ```rust pub fn cross_with(&self, other: Self) -> Result<Self, CrossRateError> ``` `Money::checked_add(&self, other: Money)` reads the same way. Having just settled the argument side, the receiver is what is left of the question #10 asked: a reader still has no rule to carry from one call to the next. Everything on `Money`, `Currency`, `ExchangeRate`, `IsoNumericCode`, and `Parser::parse` is a candidate. One method cannot follow, and should stay as it is: `IsoAlphabeticCode::as_str` hands back a `&str` borrowed from the code's own bytes, so it needs a receiver that outlives the call. Clippy has nothing to say here — `trivially_copy_pass_by_ref` leaves receivers alone, and a pedantic run is clean — so this is a design choice rather than a lint to satisfy. Breaking for anyone holding a `&Money` and calling a method on it directly, though auto-deref covers most call sites unchanged.
rosa closed this issue 2026-08-14 22:39:06 +00:00
Sign in to join this conversation.
No labels
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#15
No description provided.