Skip to content

Decimal constructor is private #1155

@archseer

Description

@archseer

The comment on the Mul implementation says

/// Both d*u and u*d with d: Decimal and u: Uint128 returns an Uint128. There is no
/// specific reason for this decision other than the initial use cases we have. If you
/// need a Decimal result for the same calculation, use Decimal(d*u) or Decimal(u*d).

but the constructor Decimal(_) is private.

Because of this there is also no way to construct a decimal statically, let's say if I wanted to I pre-calculate some constants at compile time and I already pre-scaled the value correctly, i.e.

pub const fn decimal(i: usize, decimals: u8) -> u128 {
    let decimals = 10u128.pow(decimals as u32);
    i as u128 * decimals
}

const MY_NUM: Decimal = Decimal(decimal(10, 18));

and it's impossible to convert a raw u128 value without going through from_ratio:

let raw = some_decimal.numerator();
// none of the constants are public so we have to re-declare
const DECIMAL_FRACTIONAL: Uint128 = Uint128::new(1_000_000_000_000_000_000u128); 
Decimal::from_ratio(raw, DECIMAL_FRACTIONAL)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions