Intoroduce PyComplex with minimum APIs
This commit is contained in:
parent
f9bca1aebe
commit
c6d588877b
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
pub use self::boolobject::PyBool;
|
pub use self::boolobject::PyBool;
|
||||||
pub use self::bytearray::PyByteArray;
|
pub use self::bytearray::PyByteArray;
|
||||||
|
pub use self::complex::PyComplex;
|
||||||
pub use self::datetime::PyDeltaAccess;
|
pub use self::datetime::PyDeltaAccess;
|
||||||
pub use self::datetime::{PyDate, PyDateTime, PyDelta, PyTime, PyTzInfo};
|
pub use self::datetime::{PyDate, PyDateTime, PyDelta, PyTime, PyTzInfo};
|
||||||
pub use self::datetime::{PyDateAccess, PyTimeAccess};
|
pub use self::datetime::{PyDateAccess, PyTimeAccess};
|
||||||
|
@ -184,6 +185,7 @@ pyobject_native_type_convert!(PyObjectRef, ffi::PyBaseObject_Type, ffi::PyObject
|
||||||
|
|
||||||
mod boolobject;
|
mod boolobject;
|
||||||
mod bytearray;
|
mod bytearray;
|
||||||
|
mod complex;
|
||||||
mod datetime;
|
mod datetime;
|
||||||
mod dict;
|
mod dict;
|
||||||
pub mod exc;
|
pub mod exc;
|
||||||
|
|
Loading…
Reference in New Issue