Skip to main content

decimal_scaled/types/
rescale.rs

1// SPDX-FileCopyrightText: 2026 John Moxley
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Scale-changing operations for every decimal width.
5//!
6//! Each `Dxx<SCALE>` carries its scale in the type. Converting between
7//! two scales — for instance accumulating cents (`D38<2>`) into a
8//! picometre-precision running total (`D38<12>`) — requires an explicit
9//! rescale.
10//!
11//! Two surfaces, emitted on every width:
12//!
13//! - `rescale::<TARGET>()` — a `const fn` shorthand that uses the
14//!   crate-default rounding mode (`HalfToEven` unless overridden by a
15//!   `rounding-*` Cargo feature). Suitable for the overwhelming
16//!   majority of cases.
17//! - `rescale_with::<TARGET>(mode)` — takes an explicit
18//!   [`crate::support::rounding::RoundingMode`] for users whose accounting rules
19//!   mandate a non-default rule.
20//! - `with_scale::<TARGET>()` — builder-style alias for `rescale`.
21//!
22//! Scale-up direction (target > source) is always exact: the stored
23//! integer is multiplied by `10^diff`. Scale-down direction (target <
24//! source) discards fractional digits using the requested rounding
25//! mode.
26//!
27//! Overflow on the scale-up direction is detected via `checked_mul`
28//! and panics with a clear message in both debug and release builds.
29
30use crate::types::widths::{D18, D38};
31
32// The rescale / rescale_with methods are emitted by
33// `crate::macros::rescale::decl_decimal_rescale!` — same macro for
34// every width; wide tiers receive it from `macros::full`.
35crate::macros::rescale::decl_decimal_rescale!(wide D38, crate::int::types::Int<2>);
36crate::macros::rescale::decl_decimal_rescale!(wide D18, crate::int::types::Int<1>);