1- //! Implements conversion utitlities.
21use crate :: npyffi:: { NpyTypes , PyArray_Descr , NPY_TYPES , PY_ARRAY_API } ;
32pub use num_complex:: Complex32 as c32;
43pub use num_complex:: Complex64 as c64;
@@ -8,6 +7,21 @@ use pyo3::types::PyType;
87use pyo3:: { AsPyPointer , PyNativeType } ;
98use std:: os:: raw:: c_int;
109
10+ /// Binding of [`numpy.dtype`](https://numpy.org/doc/stable/reference/generated/numpy.dtype.html).
11+ ///
12+ /// # Example
13+ /// ```
14+ /// use pyo3::types::IntoPyDict;
15+ /// pyo3::Python::with_gil(|py| {
16+ /// let locals = [("np", numpy::get_array_module(py).unwrap())].into_py_dict(py);
17+ /// let dtype: &numpy::PyArrayDescr = py
18+ /// .eval("np.array([1, 2, 3.0]).dtype", Some(locals), None)
19+ /// .unwrap()
20+ /// .downcast()
21+ /// .unwrap();
22+ /// assert_eq!(dtype.get_datatype().unwrap(), numpy::DataType::Float64);
23+ /// });
24+ /// ```
1125pub struct PyArrayDescr ( PyAny ) ;
1226
1327pyobject_native_type_core ! (
@@ -28,34 +42,48 @@ unsafe fn arraydescr_check(op: *mut ffi::PyObject) -> c_int {
2842}
2943
3044impl PyArrayDescr {
45+ /// Returns `self` as `*mut PyArray_Descr`.
3146 pub fn as_dtype_ptr ( & self ) -> * mut PyArray_Descr {
3247 self . as_ptr ( ) as _
3348 }
3449
50+ /// Returns the internal `PyType` that this `dtype` holds.
51+ ///
52+ /// # Example
53+ /// ```
54+ /// pyo3::Python::with_gil(|py| {
55+ /// let array = numpy::PyArray::from_vec(py, vec![0.0, 1.0, 2.0f64]);
56+ /// let dtype = array.dtype();
57+ /// assert_eq!(dtype.get_type().name().to_string(), "numpy.float64");
58+ /// });
59+ /// ```
3560 pub fn get_type ( & self ) -> & PyType {
3661 let dtype_type_ptr = unsafe { * self . as_dtype_ptr ( ) } . typeobj ;
3762 unsafe { PyType :: from_type_ptr ( self . py ( ) , dtype_type_ptr) }
3863 }
3964
40- pub fn get_typenum ( & self ) -> std:: os:: raw:: c_int {
41- unsafe { * self . as_dtype_ptr ( ) } . type_num
42- }
43-
65+ /// Returns the data type as `DataType` enum.
4466 pub fn get_datatype ( & self ) -> Option < DataType > {
4567 DataType :: from_typenum ( self . get_typenum ( ) )
4668 }
4769
48- pub fn from_npy_type ( py : Python , npy_type : NPY_TYPES ) -> & Self {
70+ fn from_npy_type ( py : Python , npy_type : NPY_TYPES ) -> & Self {
4971 unsafe {
5072 let descr = PY_ARRAY_API . PyArray_DescrFromType ( npy_type as i32 ) ;
5173 py. from_owned_ptr ( descr as _ )
5274 }
5375 }
76+
77+ fn get_typenum ( & self ) -> std:: os:: raw:: c_int {
78+ unsafe { * self . as_dtype_ptr ( ) } . type_num
79+ }
5480}
5581
56- /// An enum type represents numpy data type.
82+ /// Represents numpy data type.
5783///
58- /// This type is mainly for displaying error, and user don't have to use it directly.
84+ /// This is an incomplete counterpart of
85+ /// [Enumerated Types](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types)
86+ /// in numpy C-API.
5987#[ derive( Clone , Debug , Eq , PartialEq ) ]
6088pub enum DataType {
6189 Bool ,
@@ -75,6 +103,8 @@ pub enum DataType {
75103}
76104
77105impl DataType {
106+ /// Construct `DataType` from
107+ /// [Enumerated Types](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
78108 pub fn from_typenum ( typenum : c_int ) -> Option < Self > {
79109 Some ( match typenum {
80110 x if x == NPY_TYPES :: NPY_BOOL as i32 => DataType :: Bool ,
@@ -97,11 +127,8 @@ impl DataType {
97127 } )
98128 }
99129
100- pub fn from_dtype ( dtype : & crate :: PyArrayDescr ) -> Option < Self > {
101- Self :: from_typenum ( dtype. get_typenum ( ) )
102- }
103-
104- #[ inline]
130+ /// Convert `self` into
131+ /// [Enumerated Types](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
105132 pub fn into_ctype ( self ) -> NPY_TYPES {
106133 match self {
107134 DataType :: Bool => NPY_TYPES :: NPY_BOOL ,
@@ -143,15 +170,20 @@ impl DataType {
143170
144171/// Represents that a type can be an element of `PyArray`.
145172pub trait Element : Clone {
173+ /// `DataType` corresponding to this type.
146174 const DATA_TYPE : DataType ;
147175
176+ /// Returns if the give `dtype` is convertible to `Self` in Rust.
148177 fn is_same_type ( dtype : & PyArrayDescr ) -> bool ;
149178
179+ /// Returns the corresponding
180+ /// [Enumerated Type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
150181 #[ inline]
151182 fn npy_type ( ) -> NPY_TYPES {
152183 Self :: DATA_TYPE . into_ctype ( )
153184 }
154185
186+ /// Create `dtype`.
155187 fn get_dtype ( py : Python ) -> & PyArrayDescr {
156188 PyArrayDescr :: from_npy_type ( py, Self :: npy_type ( ) )
157189 }
0 commit comments