// Copyright © 2018–2021 Trevor Spiteri // 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 // and // . use crate::{ types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8}, FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64, FixedU8, }; use az::{Cast, CheckedCast, OverflowingCast, SaturatingCast, UnwrappedCast, WrappingCast}; #[cfg(feature = "f16")] use half::{bf16, f16}; macro_rules! cast { ($Src:ident($LeEqUSrc:ident); $Dst:ident($LeEqUDst:ident)) => { impl Cast<$Dst> for $Src { #[inline] fn cast(self) -> $Dst { self.to_num() } } impl CheckedCast<$Dst> for $Src { #[inline] fn checked_cast(self) -> Option<$Dst> { self.checked_to_num() } } impl SaturatingCast<$Dst> for $Src { #[inline] fn saturating_cast(self) -> $Dst { self.saturating_to_num() } } impl WrappingCast<$Dst> for $Src { #[inline] fn wrapping_cast(self) -> $Dst { self.wrapping_to_num() } } impl OverflowingCast<$Dst> for $Src { #[inline] fn overflowing_cast(self) -> ($Dst, bool) { self.overflowing_to_num() } } impl UnwrappedCast<$Dst> for $Src { #[inline] fn unwrapped_cast(self) -> $Dst { self.unwrapped_to_num() } } }; ($Fixed:ident($LeEqU:ident); $Dst:ident) => { impl Cast<$Dst> for $Fixed { #[inline] fn cast(self) -> $Dst { self.to_num() } } impl CheckedCast<$Dst> for $Fixed { #[inline] fn checked_cast(self) -> Option<$Dst> { self.checked_to_num() } } impl SaturatingCast<$Dst> for $Fixed { #[inline] fn saturating_cast(self) -> $Dst { self.saturating_to_num() } } impl WrappingCast<$Dst> for $Fixed { #[inline] fn wrapping_cast(self) -> $Dst { self.wrapping_to_num() } } impl OverflowingCast<$Dst> for $Fixed { #[inline] fn overflowing_cast(self) -> ($Dst, bool) { self.overflowing_to_num() } } impl UnwrappedCast<$Dst> for $Fixed { #[inline] fn unwrapped_cast(self) -> $Dst { self.unwrapped_to_num() } } }; ($Src:ident; $Fixed:ident($LeEqU:ident)) => { impl Cast<$Fixed> for $Src { #[inline] fn cast(self) -> $Fixed { <$Fixed>::from_num(self) } } impl CheckedCast<$Fixed> for $Src { #[inline] fn checked_cast(self) -> Option<$Fixed> { <$Fixed>::checked_from_num(self) } } impl SaturatingCast<$Fixed> for $Src { #[inline] fn saturating_cast(self) -> $Fixed { <$Fixed>::saturating_from_num(self) } } impl WrappingCast<$Fixed> for $Src { #[inline] fn wrapping_cast(self) -> $Fixed { <$Fixed>::wrapping_from_num(self) } } impl OverflowingCast<$Fixed> for $Src { #[inline] fn overflowing_cast(self) -> ($Fixed, bool) { <$Fixed>::overflowing_from_num(self) } } impl UnwrappedCast<$Fixed> for $Src { #[inline] fn unwrapped_cast(self) -> $Fixed { <$Fixed>::unwrapped_from_num(self) } } }; } macro_rules! cast_num { ($Src:ident($LeEqUSrc:ident); $($Dst:ident($LeEqUDst:ident),)*) => { $( cast! { $Src($LeEqUSrc); $Dst($LeEqUDst) } )* }; ($Fixed:ident($LeEqU:ident); $($Num:ident,)*) => { $( cast! { $Fixed($LeEqU); $Num } cast! { $Num; $Fixed($LeEqU) } )* }; ($($Fixed:ident($LeEqU:ident),)*) => { $( cast_num! { $Fixed($LeEqU); FixedI8(LeEqU8), FixedI16(LeEqU16), FixedI32(LeEqU32), FixedI64(LeEqU64), FixedI128(LeEqU128), FixedU8(LeEqU8), FixedU16(LeEqU16), FixedU32(LeEqU32), FixedU64(LeEqU64), FixedU128(LeEqU128), } cast! { bool; $Fixed($LeEqU) } cast_num! { $Fixed($LeEqU); i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, } #[cfg(feature = "f16")] cast_num! { $Fixed($LeEqU); f16, bf16, } )* }; } cast_num! { FixedI8(LeEqU8), FixedI16(LeEqU16), FixedI32(LeEqU32), FixedI64(LeEqU64), FixedI128(LeEqU128), FixedU8(LeEqU8), FixedU16(LeEqU16), FixedU32(LeEqU32), FixedU64(LeEqU64), FixedU128(LeEqU128), }