1 use super::Complex;
2 use num_traits::{AsPrimitive, FromPrimitive, Num, NumCast, ToPrimitive};
3 
4 macro_rules! impl_to_primitive {
5     ($ty:ty, $to:ident) => {
6         #[inline]
7         fn $to(&self) -> Option<$ty> {
8             if self.im.is_zero() {
9                 self.re.$to()
10             } else {
11                 None
12             }
13         }
14     };
15 } // impl_to_primitive
16 
17 // Returns None if Complex part is non-zero
18 impl<T: ToPrimitive + Num> ToPrimitive for Complex<T> {
19     impl_to_primitive!(usize, to_usize);
20     impl_to_primitive!(isize, to_isize);
21     impl_to_primitive!(u8, to_u8);
22     impl_to_primitive!(u16, to_u16);
23     impl_to_primitive!(u32, to_u32);
24     impl_to_primitive!(u64, to_u64);
25     impl_to_primitive!(i8, to_i8);
26     impl_to_primitive!(i16, to_i16);
27     impl_to_primitive!(i32, to_i32);
28     impl_to_primitive!(i64, to_i64);
29     impl_to_primitive!(u128, to_u128);
30     impl_to_primitive!(i128, to_i128);
31     impl_to_primitive!(f32, to_f32);
32     impl_to_primitive!(f64, to_f64);
33 }
34 
35 macro_rules! impl_from_primitive {
36     ($ty:ty, $from_xx:ident) => {
37         #[inline]
38         fn $from_xx(n: $ty) -> Option<Self> {
39             Some(Complex {
40                 re: T::$from_xx(n)?,
41                 im: T::zero(),
42             })
43         }
44     };
45 } // impl_from_primitive
46 
47 impl<T: FromPrimitive + Num> FromPrimitive for Complex<T> {
48     impl_from_primitive!(usize, from_usize);
49     impl_from_primitive!(isize, from_isize);
50     impl_from_primitive!(u8, from_u8);
51     impl_from_primitive!(u16, from_u16);
52     impl_from_primitive!(u32, from_u32);
53     impl_from_primitive!(u64, from_u64);
54     impl_from_primitive!(i8, from_i8);
55     impl_from_primitive!(i16, from_i16);
56     impl_from_primitive!(i32, from_i32);
57     impl_from_primitive!(i64, from_i64);
58     impl_from_primitive!(u128, from_u128);
59     impl_from_primitive!(i128, from_i128);
60     impl_from_primitive!(f32, from_f32);
61     impl_from_primitive!(f64, from_f64);
62 }
63 
64 impl<T: NumCast + Num> NumCast for Complex<T> {
from<U: ToPrimitive>(n: U) -> Option<Self>65     fn from<U: ToPrimitive>(n: U) -> Option<Self> {
66         Some(Complex {
67             re: T::from(n)?,
68             im: T::zero(),
69         })
70     }
71 }
72 
73 impl<T, U> AsPrimitive<U> for Complex<T>
74 where
75     T: AsPrimitive<U>,
76     U: 'static + Copy,
77 {
as_(self) -> U78     fn as_(self) -> U {
79         self.re.as_()
80     }
81 }
82 
83 #[cfg(test)]
84 mod test {
85     use super::*;
86 
87     #[test]
test_to_primitive()88     fn test_to_primitive() {
89         let a: Complex<u32> = Complex { re: 3, im: 0 };
90         assert_eq!(a.to_i32(), Some(3_i32));
91         let b: Complex<u32> = Complex { re: 3, im: 1 };
92         assert_eq!(b.to_i32(), None);
93         let x: Complex<f32> = Complex { re: 1.0, im: 0.1 };
94         assert_eq!(x.to_f32(), None);
95         let y: Complex<f32> = Complex { re: 1.0, im: 0.0 };
96         assert_eq!(y.to_f32(), Some(1.0));
97         let z: Complex<f32> = Complex { re: 1.0, im: 0.0 };
98         assert_eq!(z.to_i32(), Some(1));
99     }
100 
101     #[test]
test_from_primitive()102     fn test_from_primitive() {
103         let a: Complex<f32> = FromPrimitive::from_i32(2).unwrap();
104         assert_eq!(a, Complex { re: 2.0, im: 0.0 });
105     }
106 
107     #[test]
test_num_cast()108     fn test_num_cast() {
109         let a: Complex<f32> = NumCast::from(2_i32).unwrap();
110         assert_eq!(a, Complex { re: 2.0, im: 0.0 });
111     }
112 
113     #[test]
test_as_primitive()114     fn test_as_primitive() {
115         let a: Complex<f32> = Complex { re: 2.0, im: 0.2 };
116         let a_: i32 = a.as_();
117         assert_eq!(a_, 2_i32);
118     }
119 }
120