2021-02-05 06:22:27 -08:00
|
|
|
|
// Copyright © 2018–2021 Trevor Spiteri
|
2019-10-10 07:47:10 -07:00
|
|
|
|
|
|
|
|
|
// This library is free software: you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of either
|
|
|
|
|
//
|
|
|
|
|
// * the Apache License, Version 2.0 or
|
|
|
|
|
// * the MIT License
|
|
|
|
|
//
|
|
|
|
|
// at your option.
|
|
|
|
|
//
|
|
|
|
|
// You should have recieved copies of the Apache License and the MIT
|
|
|
|
|
// License along with the library. If not, see
|
|
|
|
|
// <https://www.apache.org/licenses/LICENSE-2.0> and
|
|
|
|
|
// <https://opensource.org/licenses/MIT>.
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8},
|
|
|
|
|
FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64,
|
|
|
|
|
FixedU8,
|
|
|
|
|
};
|
2021-02-03 02:17:52 -08:00
|
|
|
|
use az::{Cast, CheckedCast, OverflowingCast, SaturatingCast, UnwrappedCast, WrappingCast};
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[cfg(feature = "f16")]
|
2019-10-16 02:32:53 -07:00
|
|
|
|
use half::{bf16, f16};
|
2019-10-10 07:47:10 -07:00
|
|
|
|
|
2020-05-01 09:28:17 -07:00
|
|
|
|
macro_rules! cast {
|
2019-10-10 10:28:16 -07:00
|
|
|
|
($Src:ident($LeEqUSrc:ident); $Dst:ident($LeEqUDst:ident)) => {
|
|
|
|
|
impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> Cast<$Dst<FracDst>> for $Src<FracSrc> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn cast(self) -> $Dst<FracDst> {
|
|
|
|
|
self.to_num()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> CheckedCast<$Dst<FracDst>> for $Src<FracSrc> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn checked_cast(self) -> Option<$Dst<FracDst>> {
|
|
|
|
|
self.checked_to_num()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> SaturatingCast<$Dst<FracDst>>
|
|
|
|
|
for $Src<FracSrc>
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn saturating_cast(self) -> $Dst<FracDst> {
|
|
|
|
|
self.saturating_to_num()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> WrappingCast<$Dst<FracDst>> for $Src<FracSrc> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn wrapping_cast(self) -> $Dst<FracDst> {
|
|
|
|
|
self.wrapping_to_num()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> OverflowingCast<$Dst<FracDst>>
|
|
|
|
|
for $Src<FracSrc>
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn overflowing_cast(self) -> ($Dst<FracDst>, bool) {
|
|
|
|
|
self.overflowing_to_num()
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-03 02:17:52 -08:00
|
|
|
|
|
|
|
|
|
impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> UnwrappedCast<$Dst<FracDst>>
|
|
|
|
|
for $Src<FracSrc>
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn unwrapped_cast(self) -> $Dst<FracDst> {
|
|
|
|
|
self.unwrapped_to_num()
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-10 10:28:16 -07:00
|
|
|
|
};
|
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
($Fixed:ident($LeEqU:ident); $Dst:ident) => {
|
|
|
|
|
impl<Frac: $LeEqU> Cast<$Dst> for $Fixed<Frac> {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn cast(self) -> $Dst {
|
|
|
|
|
self.to_num()
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
impl<Frac: $LeEqU> CheckedCast<$Dst> for $Fixed<Frac> {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn checked_cast(self) -> Option<$Dst> {
|
|
|
|
|
self.checked_to_num()
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
impl<Frac: $LeEqU> SaturatingCast<$Dst> for $Fixed<Frac> {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn saturating_cast(self) -> $Dst {
|
|
|
|
|
self.saturating_to_num()
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
impl<Frac: $LeEqU> WrappingCast<$Dst> for $Fixed<Frac> {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn wrapping_cast(self) -> $Dst {
|
|
|
|
|
self.wrapping_to_num()
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
impl<Frac: $LeEqU> OverflowingCast<$Dst> for $Fixed<Frac> {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn overflowing_cast(self) -> ($Dst, bool) {
|
|
|
|
|
self.overflowing_to_num()
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-03 02:17:52 -08:00
|
|
|
|
|
|
|
|
|
impl<Frac: $LeEqU> UnwrappedCast<$Dst> for $Fixed<Frac> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn unwrapped_cast(self) -> $Dst {
|
|
|
|
|
self.unwrapped_to_num()
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-10 10:57:44 -07:00
|
|
|
|
};
|
2019-10-10 07:47:10 -07:00
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
($Src:ident; $Fixed:ident($LeEqU:ident)) => {
|
|
|
|
|
impl<Frac: $LeEqU> Cast<$Fixed<Frac>> for $Src {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn cast(self) -> $Fixed<Frac> {
|
|
|
|
|
<$Fixed<Frac>>::from_num(self)
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
impl<Frac: $LeEqU> CheckedCast<$Fixed<Frac>> for $Src {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn checked_cast(self) -> Option<$Fixed<Frac>> {
|
|
|
|
|
<$Fixed<Frac>>::checked_from_num(self)
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
impl<Frac: $LeEqU> SaturatingCast<$Fixed<Frac>> for $Src {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn saturating_cast(self) -> $Fixed<Frac> {
|
|
|
|
|
<$Fixed<Frac>>::saturating_from_num(self)
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
impl<Frac: $LeEqU> WrappingCast<$Fixed<Frac>> for $Src {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn wrapping_cast(self) -> $Fixed<Frac> {
|
|
|
|
|
<$Fixed<Frac>>::wrapping_from_num(self)
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 10:57:44 -07:00
|
|
|
|
impl<Frac: $LeEqU> OverflowingCast<$Fixed<Frac>> for $Src {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
#[inline]
|
2019-10-10 10:57:44 -07:00
|
|
|
|
fn overflowing_cast(self) -> ($Fixed<Frac>, bool) {
|
|
|
|
|
<$Fixed<Frac>>::overflowing_from_num(self)
|
2019-10-10 07:47:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-03 02:17:52 -08:00
|
|
|
|
|
|
|
|
|
impl<Frac: $LeEqU> UnwrappedCast<$Fixed<Frac>> for $Src {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn unwrapped_cast(self) -> $Fixed<Frac> {
|
|
|
|
|
<$Fixed<Frac>>::unwrapped_from_num(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-10 07:47:10 -07:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 09:28:17 -07:00
|
|
|
|
macro_rules! cast_num {
|
2019-10-10 10:28:16 -07:00
|
|
|
|
($Src:ident($LeEqUSrc:ident); $($Dst:ident($LeEqUDst:ident),)*) => { $(
|
2020-05-01 09:28:17 -07:00
|
|
|
|
cast! { $Src($LeEqUSrc); $Dst($LeEqUDst) }
|
2019-10-10 10:28:16 -07:00
|
|
|
|
)* };
|
2019-10-10 07:47:10 -07:00
|
|
|
|
($Fixed:ident($LeEqU:ident); $($Num:ident,)*) => { $(
|
2020-05-01 09:28:17 -07:00
|
|
|
|
cast! { $Fixed($LeEqU); $Num }
|
|
|
|
|
cast! { $Num; $Fixed($LeEqU) }
|
2019-10-10 07:47:10 -07:00
|
|
|
|
)* };
|
|
|
|
|
($($Fixed:ident($LeEqU:ident),)*) => { $(
|
2020-05-01 09:28:17 -07:00
|
|
|
|
cast_num! {
|
2019-10-10 14:31:05 -07:00
|
|
|
|
$Fixed($LeEqU);
|
|
|
|
|
FixedI8(LeEqU8), FixedI16(LeEqU16), FixedI32(LeEqU32), FixedI64(LeEqU64),
|
|
|
|
|
FixedI128(LeEqU128),
|
|
|
|
|
FixedU8(LeEqU8), FixedU16(LeEqU16), FixedU32(LeEqU32), FixedU64(LeEqU64),
|
|
|
|
|
FixedU128(LeEqU128),
|
|
|
|
|
}
|
2020-05-01 09:28:17 -07:00
|
|
|
|
cast! { bool; $Fixed($LeEqU) }
|
|
|
|
|
cast_num! {
|
2019-10-10 14:31:05 -07:00
|
|
|
|
$Fixed($LeEqU);
|
|
|
|
|
i8, i16, i32, i64, i128, isize,
|
|
|
|
|
u8, u16, u32, u64, u128, usize,
|
|
|
|
|
f32, f64,
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature = "f16")]
|
2020-05-01 09:28:17 -07:00
|
|
|
|
cast_num! {
|
2019-10-10 14:31:05 -07:00
|
|
|
|
$Fixed($LeEqU);
|
2019-10-16 02:32:53 -07:00
|
|
|
|
f16, bf16,
|
2019-10-10 14:31:05 -07:00
|
|
|
|
}
|
2019-10-10 07:47:10 -07:00
|
|
|
|
)* };
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 09:28:17 -07:00
|
|
|
|
cast_num! {
|
2019-10-10 07:47:10 -07:00
|
|
|
|
FixedI8(LeEqU8), FixedI16(LeEqU16), FixedI32(LeEqU32), FixedI64(LeEqU64), FixedI128(LeEqU128),
|
|
|
|
|
FixedU8(LeEqU8), FixedU16(LeEqU16), FixedU32(LeEqU32), FixedU64(LeEqU64), FixedU128(LeEqU128),
|
|
|
|
|
}
|