rigetti_pyo3/to_python/
complex.rs1use std::ffi::c_double;
16
17use num_complex::Complex;
18use num_traits::{Float, FloatConst};
19use pyo3::IntoPy;
20use pyo3::{types::PyComplex, Py, PyAny, PyResult, Python};
21
22use crate::{impl_for_self, ToPython};
23
24impl_for_self!(Py<PyComplex>);
25
26#[cfg(feature = "complex")]
27impl<F> ToPython<Py<PyComplex>> for &Complex<F>
28where
29 F: Copy + Float + FloatConst + Into<c_double>,
30{
31 fn to_python(&self, py: Python) -> PyResult<Py<PyComplex>> {
32 Ok(PyComplex::from_complex(py, **self).into_py(py))
33 }
34}
35
36#[cfg(feature = "complex")]
37impl<F> ToPython<Py<PyComplex>> for Complex<F>
38where
39 F: Copy + Float + FloatConst + Into<c_double>,
40{
41 fn to_python(&self, py: Python) -> PyResult<Py<PyComplex>> {
42 <&Self as ToPython<Py<PyComplex>>>::to_python(&self, py)
43 }
44}
45
46#[cfg(feature = "complex")]
47impl<F> ToPython<Py<PyAny>> for &Complex<F>
48where
49 F: Copy + Float + FloatConst + Into<c_double>,
50{
51 fn to_python(&self, py: Python) -> PyResult<Py<PyAny>> {
52 Ok(PyComplex::from_complex(py, **self).into_py(py))
53 }
54}
55
56#[cfg(feature = "complex")]
57impl<F> ToPython<Py<PyAny>> for Complex<F>
58where
59 F: Copy + Float + FloatConst + Into<c_double>,
60{
61 fn to_python(&self, py: Python) -> PyResult<Py<PyAny>> {
62 <&Self as ToPython<Py<PyAny>>>::to_python(&self, py)
63 }
64}