use crate::gaussian_integer::GaussianInteger;
use crate::integer::Integer;
use malachite_base::num::conversion::traits::ConvertibleFrom;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PrimitiveFloatFromGaussianIntegerError;
fn try_from_helper<'a, T: TryFrom<&'a Integer>>(
x: &'a GaussianInteger,
) -> Result<T, PrimitiveFloatFromGaussianIntegerError> {
if x.imaginary == 0u32 {
T::try_from(&x.real).map_err(|_| PrimitiveFloatFromGaussianIntegerError)
} else {
Err(PrimitiveFloatFromGaussianIntegerError)
}
}
fn convertible_from_helper<T: for<'a> ConvertibleFrom<&'a Integer>>(x: &GaussianInteger) -> bool {
x.imaginary == 0u32 && T::convertible_from(&x.real)
}
macro_rules! float_impls {
($f: ident) => {
impl TryFrom<&GaussianInteger> for $f {
type Error = PrimitiveFloatFromGaussianIntegerError;
#[inline]
fn try_from(x: &GaussianInteger) -> Result<$f, Self::Error> {
try_from_helper(x)
}
}
impl ConvertibleFrom<&GaussianInteger> for $f {
#[inline]
fn convertible_from(x: &GaussianInteger) -> bool {
convertible_from_helper::<$f>(x)
}
}
};
}
apply_to_primitive_floats!(float_impls);