diff --git a/src/objects/complex.rs b/src/objects/complex.rs new file mode 100644 index 00000000..49cf4466 --- /dev/null +++ b/src/objects/complex.rs @@ -0,0 +1,39 @@ +use ffi; +use object::PyObject; +use python::{Python, ToPyPointer}; +use std::os::raw::c_double; + +/// Represents a Python `complex`. +#[repr(transparent)] +pub struct PyComplex(PyObject); + +pyobject_native_type!(PyComplex, ffi::PyComplex_Type, ffi::PyComplex_Check); + +impl PyComplex { + pub fn from_doubles<'p>(py: Python<'p>, real: c_double, imag: c_double) -> &'p PyComplex { + unsafe { + let ptr = ffi::PyComplex_FromDoubles(real, imag); + py.from_owned_ptr(ptr) + } + } + pub fn real(&self) -> c_double { + unsafe { ffi::PyComplex_RealAsDouble(self.as_ptr()) } + } + pub fn imag(&self) -> c_double { + unsafe { ffi::PyComplex_ImagAsDouble(self.as_ptr()) } + } +} + +#[cfg(test)] +mod test { + use super::PyComplex; + use python::Python; + #[test] + fn test_from_double() { + let gil = Python::acquire_gil(); + let py = gil.python(); + let complex = PyComplex::from_doubles(py, 3.0, 1.2); + assert_eq!(complex.real(), 3.0); + assert_eq!(complex.imag(), 1.2); + } +} diff --git a/src/objects/mod.rs b/src/objects/mod.rs index 5966543d..8c4c9685 100644 --- a/src/objects/mod.rs +++ b/src/objects/mod.rs @@ -2,6 +2,7 @@ pub use self::boolobject::PyBool; pub use self::bytearray::PyByteArray; +pub use self::complex::PyComplex; pub use self::datetime::PyDeltaAccess; pub use self::datetime::{PyDate, PyDateTime, PyDelta, PyTime, PyTzInfo}; pub use self::datetime::{PyDateAccess, PyTimeAccess}; @@ -184,6 +185,7 @@ pyobject_native_type_convert!(PyObjectRef, ffi::PyBaseObject_Type, ffi::PyObject mod boolobject; mod bytearray; +mod complex; mod datetime; mod dict; pub mod exc;