#[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),
}
}
};
}
#[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),
}
}
};
}
#[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 {
#[must_use]
#[inline]
pub const fn is_negative(self) -> bool {
self.0.is_negative()
}
#[inline]
#[track_caller]
pub const fn abs(self) -> Self {
Self(self.0.abs())
}
#[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 {
pub const ZERO: Self = Self(<$base>::ZERO);
#[inline]
pub const fn new(value: $base) -> Self {
Self(value)
}
#[inline]
pub const fn get(self) -> $base {
self.0
}
#[must_use]
#[inline]
pub const fn is_zero(self) -> bool {
self.0.is_zero()
}
#[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,
}
}
#[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,
}
}
#[inline]
pub const fn saturating_add(self, rhs: Self) -> Self {
Self(self.0.saturating_add(rhs.0))
}
#[inline]
pub const fn saturating_sub(self, rhs: Self) -> Self {
Self(self.0.saturating_sub(rhs.0))
}
#[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,
}
}
#[inline]
pub const fn saturating_mul_int(self, n: $int) -> Self {
Self(self.0.saturating_mul_int(n))
}
#[inline]
#[track_caller]
pub fn round_to(self, step: Self, mode: $crate::Round) -> Self {
Self(self.0.round_to(step.0, mode))
}
#[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,
}
}
#[inline]
#[track_caller]
pub fn round_dp(self, places: u32, mode: $crate::Round) -> Self {
Self(self.0.round_dp(places, mode))
}
#[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,
}
}
#[inline]
#[track_caller]
pub fn floor(self) -> Self {
Self(self.0.floor())
}
#[must_use]
#[inline]
pub fn checked_floor(self) -> Option<Self> {
self.0.checked_floor().map(Self)
}
#[inline]
#[track_caller]
pub fn ceil(self) -> Self {
Self(self.0.ceil())
}
#[must_use]
#[inline]
pub fn checked_ceil(self) -> Option<Self> {
self.0.checked_ceil().map(Self)
}
#[inline]
pub const fn trunc(self) -> Self {
Self(self.0.trunc())
}
#[inline]
pub const fn fract(self) -> Self {
Self(self.0.fract())
}
#[must_use]
#[inline]
#[track_caller]
pub const fn div_euclid(self, rhs: Self) -> $raw {
self.0.div_euclid(rhs.0)
}
#[inline]
#[track_caller]
pub const fn rem_euclid(self, rhs: Self) -> Self {
Self(self.0.rem_euclid(rhs.0))
}
#[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,
}
}
#[inline]
#[track_caller]
pub fn div_int(self, n: $int, mode: $crate::Round) -> Self {
Self(self.0.div_int(n, mode))
}
#[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)
}
#[inline]
#[track_caller]
pub fn mul_dec(self, factor: $base, mode: $crate::Round) -> Self {
Self(self.0.mul(factor, mode))
}
#[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)
}
#[must_use]
#[inline]
pub const fn to_key_bytes(self) -> [u8; 16] {
self.0.to_key_bytes()
}
#[inline]
pub const fn from_key_bytes(bytes: [u8; 16]) -> Self {
Self(<$base>::from_key_bytes(bytes))
}
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),
}
}
#[inline]
pub fn write_ascii(
self,
out: &mut [u8],
) -> ::core::result::Result<usize, $crate::BufferTooSmall> {
self.0.write_ascii(out)
}
#[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)
}
#[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 }
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __newtype_features {
($name:ident, $base:ty) => {};
}