1
- //! Implements conversion utitlities.
2
1
use crate :: npyffi:: { NpyTypes , PyArray_Descr , NPY_TYPES , PY_ARRAY_API } ;
3
2
pub use num_complex:: Complex32 as c32;
4
3
pub use num_complex:: Complex64 as c64;
@@ -8,6 +7,21 @@ use pyo3::types::PyType;
8
7
use pyo3:: { AsPyPointer , PyNativeType } ;
9
8
use std:: os:: raw:: c_int;
10
9
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
+ /// ```
11
25
pub struct PyArrayDescr ( PyAny ) ;
12
26
13
27
pyobject_native_type_core ! (
@@ -28,34 +42,48 @@ unsafe fn arraydescr_check(op: *mut ffi::PyObject) -> c_int {
28
42
}
29
43
30
44
impl PyArrayDescr {
45
+ /// Returns `self` as `*mut PyArray_Descr`.
31
46
pub fn as_dtype_ptr ( & self ) -> * mut PyArray_Descr {
32
47
self . as_ptr ( ) as _
33
48
}
34
49
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
+ /// ```
35
60
pub fn get_type ( & self ) -> & PyType {
36
61
let dtype_type_ptr = unsafe { * self . as_dtype_ptr ( ) } . typeobj ;
37
62
unsafe { PyType :: from_type_ptr ( self . py ( ) , dtype_type_ptr) }
38
63
}
39
64
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.
44
66
pub fn get_datatype ( & self ) -> Option < DataType > {
45
67
DataType :: from_typenum ( self . get_typenum ( ) )
46
68
}
47
69
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 {
49
71
unsafe {
50
72
let descr = PY_ARRAY_API . PyArray_DescrFromType ( npy_type as i32 ) ;
51
73
py. from_owned_ptr ( descr as _ )
52
74
}
53
75
}
76
+
77
+ fn get_typenum ( & self ) -> std:: os:: raw:: c_int {
78
+ unsafe { * self . as_dtype_ptr ( ) } . type_num
79
+ }
54
80
}
55
81
56
- /// An enum type represents numpy data type.
82
+ /// Represents numpy data type.
57
83
///
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.
59
87
#[ derive( Clone , Debug , Eq , PartialEq ) ]
60
88
pub enum DataType {
61
89
Bool ,
@@ -75,6 +103,8 @@ pub enum DataType {
75
103
}
76
104
77
105
impl DataType {
106
+ /// Construct `DataType` from
107
+ /// [Enumerated Types](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
78
108
pub fn from_typenum ( typenum : c_int ) -> Option < Self > {
79
109
Some ( match typenum {
80
110
x if x == NPY_TYPES :: NPY_BOOL as i32 => DataType :: Bool ,
@@ -97,11 +127,8 @@ impl DataType {
97
127
} )
98
128
}
99
129
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).
105
132
pub fn into_ctype ( self ) -> NPY_TYPES {
106
133
match self {
107
134
DataType :: Bool => NPY_TYPES :: NPY_BOOL ,
@@ -143,15 +170,20 @@ impl DataType {
143
170
144
171
/// Represents that a type can be an element of `PyArray`.
145
172
pub trait Element : Clone {
173
+ /// `DataType` corresponding to this type.
146
174
const DATA_TYPE : DataType ;
147
175
176
+ /// Returns if the give `dtype` is convertible to `Self` in Rust.
148
177
fn is_same_type ( dtype : & PyArrayDescr ) -> bool ;
149
178
179
+ /// Returns the corresponding
180
+ /// [Enumerated Type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
150
181
#[ inline]
151
182
fn npy_type ( ) -> NPY_TYPES {
152
183
Self :: DATA_TYPE . into_ctype ( )
153
184
}
154
185
186
+ /// Create `dtype`.
155
187
fn get_dtype ( py : Python ) -> & PyArrayDescr {
156
188
PyArrayDescr :: from_npy_type ( py, Self :: npy_type ( ) )
157
189
}
0 commit comments