use half::f16;
pub trait ToStorage<T>: 'static + Copy
where
T: 'static + Copy,
{
fn to_(self) -> T;
}
macro_rules! impl_to_integral_storage {
($from:ty, $to:ty) => {
impl ToStorage<$to> for $from {
fn to_(self) -> $to {
self.round().max(0 as $from).min(<$to>::MAX as $from) as $to
}
}
};
}
impl_to_integral_storage!(f32, i8);
impl_to_integral_storage!(f64, i8);
impl_to_integral_storage!(f32, u8);
impl_to_integral_storage!(f64, u8);
impl_to_integral_storage!(f32, u16);
impl_to_integral_storage!(f64, u16);
impl_to_integral_storage!(f32, i16);
impl_to_integral_storage!(f64, i16);
impl_to_integral_storage!(f32, u32);
impl_to_integral_storage!(f64, u32);
impl_to_integral_storage!(f32, i32);
impl_to_integral_storage!(f64, i32);
impl_to_integral_storage!(f32, i64);
impl_to_integral_storage!(f64, i64);
impl_to_integral_storage!(f32, u64);
impl_to_integral_storage!(f64, u64);
impl_to_integral_storage!(f32, usize);
impl_to_integral_storage!(f64, usize);
macro_rules! impl_to_direct_storage {
($from:ty, $to:ty) => {
impl ToStorage<$to> for $from {
fn to_(self) -> $to {
self as $to
}
}
};
}
impl_to_direct_storage!(f32, f32);
impl_to_direct_storage!(f64, f64);
impl_to_direct_storage!(f64, f32);
impl_to_direct_storage!(f32, f64);
macro_rules! impl_to_saturated_signed_storage {
($from:ty, $to:ty) => {
impl ToStorage<$to> for $from {
fn to_(self) -> $to {
self.max(0).min(<$to>::MAX as $from) as $to
}
}
};
}
macro_rules! impl_to_saturated_storage {
($from:ty, $to:ty) => {
impl ToStorage<$to> for $from {
fn to_(self) -> $to {
self.min(<$to>::MAX as $from) as $to
}
}
};
}
impl_to_saturated_signed_storage!(i16, u8);
impl_to_saturated_signed_storage!(i32, u8);
impl_to_saturated_signed_storage!(i64, u8);
impl_to_saturated_signed_storage!(i32, u16);
impl_to_saturated_signed_storage!(i64, u16);
impl_to_saturated_storage!(u16, u8);
impl_to_saturated_storage!(u32, u8);
impl_to_saturated_storage!(u64, u8);
impl_to_saturated_signed_storage!(i32, i8);
impl_to_saturated_signed_storage!(i64, i8);
impl_to_saturated_signed_storage!(i32, i16);
impl_to_saturated_signed_storage!(i64, i16);
impl_to_saturated_signed_storage!(i16, i8);
impl_to_saturated_signed_storage!(u16, i8);
impl ToStorage<f16> for f32 {
fn to_(self) -> f16 {
f16::from_f32(self)
}
}
impl ToStorage<f16> for f64 {
fn to_(self) -> f16 {
f16::from_f64(self)
}
}