Skip to main content

Module primitives

Module primitives 

Source
Expand description

Domain newtypes — zero-cost wrappers that make invalid values harder to pass by accident.

§Design (see rust_design_patterns Newtype)

  • Private fields; construction is fallible (TryFrom / associated try_ constructors).
  • Not type aliases: Rate is not interchangeable with bare f64 at the type level.
  • Additive for 0.1.x / 0.2: existing free functions still take f64 / u32. Newtypes are for call sites and TA config that want compile-time clarity + one-time validation.
  • Extract with .get() / Into<f64> when calling f64-based APIs.

§When to use

TypePrefer when
RateYou validated a rate once and pass it through several calls
PeriodsPeriod counts should not mix with money amounts
PositivePriceEquity prices / volumes that must be > 0
MoneyFinite signed amounts (loans, payments) without unit claims
PeriodLengthTA lookbacks (SMA(20), stoch k_period) — usize ≥ 1

§Examples

use finance_solution::{future_value, PositivePrice, Rate, Periods, FinanceResult};

fn grow(rate: Rate, n: Periods, pv: f64) -> FinanceResult<f64> {
    future_value(rate.get(), n.get(), pv, false)
}

let r = Rate::tvm(0.05)?;
let n = Periods::new(10)?;
assert!(grow(r, n, -1_000.0).is_ok());
assert!(Rate::tvm(-1.5).is_err());
assert!(PositivePrice::new(0.0).is_err());

Structs§

Money
Finite signed monetary amount (no currency unit — pure magnitude + sign).
PeriodLength
Positive lookback / window length for technical indicators (usize ≥ 1).
Periods
Count of compounding / payment periods (u32).
PositivePrice
Strictly positive finite price (or similar quantity used as a price level).
Rate
Periodic or continuous interest / return rate as a decimal (e.g. 0.05 = 5%).