decimix 0.1.4

Fixed-point decimal arithmetic: i128 at 19 decimal places
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! `dec!`, `udec!` and `newtype!`.

/// A [`Dec19`](crate::Dec19) literal, checked at compile time.
///
/// Takes a number literal or a string literal. The literal's source text is
/// read as decimal digits, so it never passes through `f64`: `dec!(0.1)` is
/// exactly one tenth.
///
/// ```
/// use decimix::{Dec19, dec};
///
/// const TICK: Dec19 = dec!(0.005);
/// let px = dec!(113.725);
/// assert_eq!(px, dec!("113.725"));
/// assert_eq!(dec!(-0.25), -dec!(0.25));
/// assert_eq!(dec!(1_000_000), Dec19::from(1_000_000));
/// ```
///
/// A bad literal is a compile error: more than 19 decimal places, out of
/// range, an exponent (`1e-5`) or a suffix (`1.5f64`).
///
/// ```compile_fail
/// let x = decimix::dec!(1.5f64);
/// ```
///
/// ```compile_fail
/// let x = decimix::dec!(1e-5);
/// ```
///
/// ```compile_fail
/// let x = decimix::dec!(0.12345678901234567891); // 20 places
/// ```
///
/// A string literal reads exactly like [`FromStr`](core::str::FromStr), so
/// underscores are only for number literals:
///
/// ```compile_fail
/// let x = decimix::dec!("1_000");
/// ```
#[macro_export]
macro_rules! dec {
  ($lit:literal) => {
    const {
      match $crate::Dec19::__from_literal(::core::stringify!($lit)) {
        ::core::result::Result::Ok(v) => v,
        ::core::result::Result::Err(msg) => ::core::panic!("{}", msg),
      }
    }
  };
}

/// A [`UDec19`](crate::UDec19) literal, checked at compile time.
///
/// Like [`dec!`], and a negative literal is also a compile error.
///
/// ```
/// use decimix::udec;
///
/// let qty = udec!(1250.5);
/// assert_eq!(qty.to_string(), "1250.5");
/// ```
///
/// ```compile_fail
/// let x = decimix::udec!(-1);
/// ```
#[macro_export]
macro_rules! udec {
  ($lit:literal) => {
    const {
      match $crate::UDec19::__from_literal(::core::stringify!($lit)) {
        ::core::result::Result::Ok(v) => v,
        ::core::result::Result::Err(msg) => ::core::panic!("{}", msg),
      }
    }
  };
}

/// Defines a domain type (a price, a quantity, a rate…) wrapping
/// [`Dec19`](crate::Dec19) or [`UDec19`](crate::UDec19).
///
/// Distinct domain types can't be mixed by accident: with `Price` and `Qty`
/// defined this way, `price + qty` doesn't compile. Products between
/// different domain types (price × quantity → notional) are left for you to
/// define, since only you know which ones make sense.
///
/// ```
/// use decimix::{Dec19, Round, dec, newtype};
///
/// newtype! {
///   /// Commission rate.
///   pub struct FeeRate(Dec19);
/// }
///
/// let r = FeeRate::new(dec!(0.0002));
/// assert_eq!(r + r, FeeRate::new(dec!(0.0004)));
/// assert_eq!(r.get(), dec!(0.0002));
/// assert_eq!(format!("{r:?}"), "FeeRate(0.0002)");
/// ```
///
/// The base type must be written exactly `Dec19` or `UDec19`. The generated
/// type is `#[repr(transparent)]` with a private field, and gets:
/// - `new`/`get` (both `const`), `ZERO`, and `From<Type> for Dec19`;
/// - `Copy`, `Eq`, `Ord`, `Hash`, `Default`;
/// - same-type `+`, `-` (and unary `-` for `Dec19`), `%`, `+=`, `-=`, `%=`,
///   `Sum`, and `*` by an integer (`i64`, or `u64` for `UDec19`), with
///   `checked_*` and `saturating_*` versions;
/// - `round_to`, `round_dp`, `floor`, `ceil`, `trunc`, `fract`,
///   `div_euclid`, `rem_euclid`, `div_int`, and
///   `mul_dec` (multiply by a plain base-type value, e.g. a ratio);
/// - `Display`, `Debug` (`FeeRate(0.0002)`), `FromStr`, `from_ascii`,
///   `write_ascii` and `to_ascii`;
/// - order-preserving `to_key_bytes`/`from_key_bytes`.
///
/// It deliberately has **no** `f64` methods: use `.get().to_f64_lossy()`,
/// so the lint rules only need to name the base types.
#[macro_export]
macro_rules! newtype {
  (
    $(#[$meta:meta])*
    $vis:vis struct $name:ident(Dec19);
  ) => {
    $crate::__newtype_common! {
      $(#[$meta])* $vis $name, $crate::Dec19, i128, i64
    }

    impl $name {
      /// True if the value is below zero.
      #[must_use]
      #[inline]
      pub const fn is_negative(self) -> bool {
        self.0.is_negative()
      }

      /// The absolute value.
      ///
      /// # Panics
      ///
      /// For the most negative value, whose absolute value doesn't fit.
      #[inline]
      #[track_caller]
      pub const fn abs(self) -> Self {
        Self(self.0.abs())
      }

      /// Negation, or `None` on overflow.
      #[must_use]
      #[inline]
      pub const fn checked_neg(self) -> ::core::option::Option<Self> {
        match self.0.checked_neg() {
          ::core::option::Option::Some(v) => ::core::option::Option::Some(Self(v)),
          ::core::option::Option::None => ::core::option::Option::None,
        }
      }
    }

    impl ::core::ops::Neg for $name {
      type Output = Self;

      #[inline]
      #[track_caller]
      fn neg(self) -> Self {
        Self(-self.0)
      }
    }
  };

  (
    $(#[$meta:meta])*
    $vis:vis struct $name:ident(UDec19);
  ) => {
    $crate::__newtype_common! {
      $(#[$meta])* $vis $name, $crate::UDec19, u128, u64
    }
  };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __newtype_common {
  (
    $(#[$meta:meta])* $vis:vis $name:ident, $base:ty, $raw:ty, $int:ty
  ) => {
    $(#[$meta])*
    #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
    #[must_use]
    #[repr(transparent)]
    $vis struct $name($base);

    impl $name {
      /// Zero.
      pub const ZERO: Self = Self(<$base>::ZERO);

      /// Wraps a plain value.
      #[inline]
      pub const fn new(value: $base) -> Self {
        Self(value)
      }

      /// The plain value.
      #[inline]
      pub const fn get(self) -> $base {
        self.0
      }

      /// True if the value is zero.
      #[must_use]
      #[inline]
      pub const fn is_zero(self) -> bool {
        self.0.is_zero()
      }

      /// Addition, or `None` on overflow.
      #[must_use]
      #[inline]
      pub const fn checked_add(self, rhs: Self) -> ::core::option::Option<Self> {
        match self.0.checked_add(rhs.0) {
          ::core::option::Option::Some(v) => ::core::option::Option::Some(Self(v)),
          ::core::option::Option::None => ::core::option::Option::None,
        }
      }

      /// Subtraction, or `None` on overflow.
      #[must_use]
      #[inline]
      pub const fn checked_sub(self, rhs: Self) -> ::core::option::Option<Self> {
        match self.0.checked_sub(rhs.0) {
          ::core::option::Option::Some(v) => ::core::option::Option::Some(Self(v)),
          ::core::option::Option::None => ::core::option::Option::None,
        }
      }

      /// Addition, clamped to the range instead of overflowing.
      #[inline]
      pub const fn saturating_add(self, rhs: Self) -> Self {
        Self(self.0.saturating_add(rhs.0))
      }

      /// Subtraction, clamped to the range instead of overflowing.
      #[inline]
      pub const fn saturating_sub(self, rhs: Self) -> Self {
        Self(self.0.saturating_sub(rhs.0))
      }

      /// Multiplication by an integer, or `None` on overflow. Exact.
      #[must_use]
      #[inline]
      pub const fn checked_mul_int(self, n: $int) -> ::core::option::Option<Self> {
        match self.0.checked_mul_int(n) {
          ::core::option::Option::Some(v) => ::core::option::Option::Some(Self(v)),
          ::core::option::Option::None => ::core::option::Option::None,
        }
      }

      /// Multiplication by an integer, clamped to the range instead of
      /// overflowing.
      #[inline]
      pub const fn saturating_mul_int(self, n: $int) -> Self {
        Self(self.0.saturating_mul_int(n))
      }

      /// Rounds to a multiple of `step` (e.g. a tick size), using `mode`.
      #[inline]
      #[track_caller]
      pub fn round_to(self, step: Self, mode: $crate::Round) -> Self {
        Self(self.0.round_to(step.0, mode))
      }

      /// [`round_to`](Self::round_to), or `None` if `step` is not positive
      /// or the result overflows. See the base type's `checked_round_to`.
      #[must_use]
      #[inline]
      pub fn checked_round_to(
        self,
        step: Self,
        mode: $crate::Round,
      ) -> Option<Self> {
        match self.0.checked_round_to(step.0, mode) {
          Some(v) => Some(Self(v)),
          None => None,
        }
      }

      /// Rounds to `places` decimal places, using `mode`.
      #[inline]
      #[track_caller]
      pub fn round_dp(self, places: u32, mode: $crate::Round) -> Self {
        Self(self.0.round_dp(places, mode))
      }

      /// [`round_dp`](Self::round_dp), or `None` if the result overflows.
      #[must_use]
      #[inline]
      pub fn checked_round_dp(
        self,
        places: u32,
        mode: $crate::Round,
      ) -> Option<Self> {
        match self.0.checked_round_dp(places, mode) {
          Some(v) => Some(Self(v)),
          None => None,
        }
      }

      /// The largest whole number not above `self`. See the base type's
      /// `floor`.
      #[inline]
      #[track_caller]
      pub fn floor(self) -> Self {
        Self(self.0.floor())
      }

      /// [`floor`](Self::floor), or `None` if the result overflows.
      #[must_use]
      #[inline]
      pub fn checked_floor(self) -> Option<Self> {
        self.0.checked_floor().map(Self)
      }

      /// The smallest whole number not below `self`. See the base type's
      /// `ceil`.
      #[inline]
      #[track_caller]
      pub fn ceil(self) -> Self {
        Self(self.0.ceil())
      }

      /// [`ceil`](Self::ceil), or `None` if the result overflows.
      #[must_use]
      #[inline]
      pub fn checked_ceil(self) -> Option<Self> {
        self.0.checked_ceil().map(Self)
      }

      /// The whole-number part, dropping the fraction. See the base type's
      /// `trunc`.
      #[inline]
      pub const fn trunc(self) -> Self {
        Self(self.0.trunc())
      }

      /// The part after the decimal point, with the sign of `self`. See the
      /// base type's `fract`.
      #[inline]
      pub const fn fract(self) -> Self {
        Self(self.0.fract())
      }

      /// How many whole `rhs` fit in `self` (Euclidean division). Exact.
      /// See the base type's `div_euclid`.
      #[must_use]
      #[inline]
      #[track_caller]
      pub const fn div_euclid(self, rhs: Self) -> $raw {
        self.0.div_euclid(rhs.0)
      }

      /// What is left after taking whole `rhs` out of `self`. Never
      /// negative. See the base type's `rem_euclid`.
      #[inline]
      #[track_caller]
      pub const fn rem_euclid(self, rhs: Self) -> Self {
        Self(self.0.rem_euclid(rhs.0))
      }

      /// `self % rhs`, or `None` if `rhs` is zero or the division
      /// overflows. See the base type's `checked_rem`.
      #[must_use]
      #[inline]
      pub const fn checked_rem(
        self,
        rhs: Self,
      ) -> ::core::option::Option<Self> {
        match self.0.checked_rem(rhs.0) {
          ::core::option::Option::Some(v) => ::core::option::Option::Some(Self(v)),
          ::core::option::Option::None => ::core::option::Option::None,
        }
      }

      /// Division by an integer, rounded with `mode`.
      #[inline]
      #[track_caller]
      pub fn div_int(self, n: $int, mode: $crate::Round) -> Self {
        Self(self.0.div_int(n, mode))
      }

      /// Division by an integer, rounded with `mode`, or `None` if `n` is
      /// zero or the result overflows.
      #[must_use]
      #[inline]
      pub fn checked_div_int(
        self,
        n: $int,
        mode: $crate::Round,
      ) -> ::core::option::Option<Self> {
        self.0.checked_div_int(n, mode).map(Self)
      }

      /// Multiplication by a plain value (a ratio, a factor), rounded with
      /// `mode`.
      #[inline]
      #[track_caller]
      pub fn mul_dec(self, factor: $base, mode: $crate::Round) -> Self {
        Self(self.0.mul(factor, mode))
      }

      /// Multiplication by a plain value, rounded with `mode`, or `None` on
      /// overflow.
      #[must_use]
      #[inline]
      pub fn checked_mul_dec(
        self,
        factor: $base,
        mode: $crate::Round,
      ) -> ::core::option::Option<Self> {
        self.0.checked_mul(factor, mode).map(Self)
      }

      /// Order-preserving key bytes: see the base type's `to_key_bytes`.
      #[must_use]
      #[inline]
      pub const fn to_key_bytes(self) -> [u8; 16] {
        self.0.to_key_bytes()
      }

      /// Reads a value from [`to_key_bytes`](Self::to_key_bytes).
      #[inline]
      pub const fn from_key_bytes(bytes: [u8; 16]) -> Self {
        Self(<$base>::from_key_bytes(bytes))
      }

      /// Reads ASCII decimal text exactly. See the base type's
      /// `from_ascii`.
      pub const fn from_ascii(
        bytes: &[u8],
      ) -> ::core::result::Result<Self, $crate::ParseError> {
        match <$base>::from_ascii(bytes) {
          ::core::result::Result::Ok(v) => ::core::result::Result::Ok(Self(v)),
          ::core::result::Result::Err(e) => ::core::result::Result::Err(e),
        }
      }

      /// Writes the shortest exact text into `out`. See the base type's
      /// `write_ascii`.
      #[inline]
      pub fn write_ascii(
        self,
        out: &mut [u8],
      ) -> ::core::result::Result<usize, $crate::BufferTooSmall> {
        self.0.write_ascii(out)
      }

      /// Writes exactly `places` decimal places into `out`. See the base
      /// type's `write_ascii_dp`.
      #[inline]
      pub fn write_ascii_dp(
        self,
        out: &mut [u8],
        places: u32,
        mode: $crate::Round,
      ) -> ::core::result::Result<usize, $crate::BufferTooSmall> {
        self.0.write_ascii_dp(out, places, mode)
      }

      /// The shortest exact text, in a small stack buffer.
      #[must_use]
      #[inline]
      pub fn to_ascii(self) -> $crate::AsciiBuf {
        self.0.to_ascii()
      }
    }

    impl ::core::convert::From<$name> for $base {
      #[inline]
      fn from(v: $name) -> $base {
        v.0
      }
    }

    impl ::core::convert::From<$int> for $name {
      #[inline]
      fn from(n: $int) -> Self {
        Self(<$base>::from(n))
      }
    }

    impl ::core::ops::Add for $name {
      type Output = Self;

      #[inline]
      #[track_caller]
      fn add(self, rhs: Self) -> Self {
        Self(self.0 + rhs.0)
      }
    }

    impl ::core::ops::Sub for $name {
      type Output = Self;

      #[inline]
      #[track_caller]
      fn sub(self, rhs: Self) -> Self {
        Self(self.0 - rhs.0)
      }
    }

    impl ::core::ops::AddAssign for $name {
      #[inline]
      #[track_caller]
      fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
      }
    }

    impl ::core::ops::SubAssign for $name {
      #[inline]
      #[track_caller]
      fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
      }
    }

    impl ::core::ops::Rem for $name {
      type Output = Self;

      #[inline]
      #[track_caller]
      fn rem(self, rhs: Self) -> Self {
        Self(self.0 % rhs.0)
      }
    }

    impl ::core::ops::RemAssign for $name {
      #[inline]
      #[track_caller]
      fn rem_assign(&mut self, rhs: Self) {
        self.0 %= rhs.0;
      }
    }

    impl ::core::ops::Mul<$int> for $name {
      type Output = Self;

      #[inline]
      #[track_caller]
      fn mul(self, n: $int) -> Self {
        Self(self.0 * n)
      }
    }

    impl ::core::ops::Mul<$name> for $int {
      type Output = $name;

      #[inline]
      #[track_caller]
      fn mul(self, rhs: $name) -> $name {
        $name(rhs.0 * self)
      }
    }

    impl ::core::iter::Sum for $name {
      #[track_caller]
      fn sum<I: ::core::iter::Iterator<Item = Self>>(iter: I) -> Self {
        Self(iter.map(|v| v.0).sum())
      }
    }

    impl<'a> ::core::iter::Sum<&'a $name> for $name {
      #[track_caller]
      fn sum<I: ::core::iter::Iterator<Item = &'a Self>>(iter: I) -> Self {
        Self(iter.map(|v| v.0).sum())
      }
    }

    impl ::core::fmt::Display for $name {
      fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
        ::core::fmt::Display::fmt(&self.0, f)
      }
    }

    impl ::core::fmt::Debug for $name {
      fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
        ::core::write!(f, "{}({})", ::core::stringify!($name), self.0)
      }
    }

    impl ::core::str::FromStr for $name {
      type Err = $crate::ParseError;

      fn from_str(s: &str) -> ::core::result::Result<Self, Self::Err> {
        Self::from_ascii(s.as_bytes())
      }
    }

    $crate::__newtype_features! { $name, $base }
  };
}

/// Integration hook: extra impls for `newtype!` types, depending on
/// decimix's own Cargo features.
///
/// Because this macro is defined inside decimix, a `#[cfg(feature = ...)]` on
/// its definition is evaluated against decimix's features, not the calling
/// crate's. Future integrations (serde, Selecta, Cap'n Proto) add a
/// feature-gated definition here. Nothing is generated yet.
#[doc(hidden)]
#[macro_export]
macro_rules! __newtype_features {
  ($name:ident, $base:ty) => {};
}