rigetti_pyo3/to_python/
complex.rs

1// Copyright 2022 Rigetti Computing
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License./
14
15use 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}