concision_traits/
complex.rs1pub trait AsComplex<T> {
9 type Complex<A>;
10 fn as_complex(&self, real: bool) -> Self::Complex<T>;
13 fn as_re(&self) -> Self::Complex<T> {
15 self.as_complex(true)
16 }
17 fn as_im(&self) -> Self::Complex<T> {
19 self.as_complex(false)
20 }
21}
22pub trait IntoComplex<T> {
24 type Complex<A>;
25 fn into_complex(self, real: bool) -> Self::Complex<T>
28 where
29 Self: Sized;
30 fn into_re(self) -> Self::Complex<T>
32 where
33 Self: Sized,
34 {
35 self.into_complex(true)
36 }
37 fn into_im(self) -> Self::Complex<T>
39 where
40 Self: Sized,
41 {
42 self.into_complex(false)
43 }
44}
45
46#[cfg(feature = "complex")]
50mod impl_complex {
51 use super::{AsComplex, IntoComplex};
52
53 use num_complex::Complex;
54 use num_traits::Zero;
55
56 impl<T> AsComplex<T> for T
57 where
58 T: Clone + IntoComplex<T>,
59 {
60 type Complex<A> = <T as IntoComplex<T>>::Complex<A>;
61
62 fn as_complex(&self, real: bool) -> Self::Complex<T> {
63 self.clone().into_complex(real)
64 }
65 }
66
67 impl<T> IntoComplex<T> for T
68 where
69 T: Zero,
70 {
71 type Complex<A> = Complex<A>;
72
73 fn into_complex(self, real: bool) -> Self::Complex<T>
74 where
75 Self: Sized,
76 {
77 if real {
78 Complex::new(self, T::zero())
79 } else {
80 Complex::new(T::zero(), self)
81 }
82 }
83 }
84}