1
1
use std:: mem:: size_of;
2
2
use std:: os:: raw:: { c_int, c_long, c_longlong, c_short, c_uint, c_ulong, c_ulonglong, c_ushort} ;
3
3
4
- use crate :: npyffi:: { NpyTypes , PyArray_Descr , NPY_TYPES , PY_ARRAY_API } ;
5
4
use cfg_if:: cfg_if;
6
5
use num_traits:: { Bounded , Zero } ;
7
6
use pyo3:: { ffi, prelude:: * , pyobject_native_type_core, types:: PyType , AsPyPointer , PyNativeType } ;
8
7
8
+ use crate :: npyffi:: { NpyTypes , PyArray_Descr , NPY_TYPES , PY_ARRAY_API } ;
9
+
9
10
pub use num_complex:: Complex32 as c32;
10
11
pub use num_complex:: Complex64 as c64;
11
12
@@ -85,17 +86,20 @@ impl PyArrayDescr {
85
86
86
87
fn from_npy_type ( py : Python , npy_type : NPY_TYPES ) -> & Self {
87
88
unsafe {
88
- let descr = PY_ARRAY_API . PyArray_DescrFromType ( npy_type as i32 ) ;
89
+ let descr = PY_ARRAY_API . PyArray_DescrFromType ( npy_type as _ ) ;
89
90
py. from_owned_ptr ( descr as _ )
90
91
}
91
92
}
92
93
93
- pub ( crate ) fn get_typenum ( & self ) -> std:: os:: raw:: c_int {
94
+ /// Retrieves the
95
+ /// [enumerated type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types)
96
+ /// for this type descriptor.
97
+ pub fn get_typenum ( & self ) -> c_int {
94
98
unsafe { * self . as_dtype_ptr ( ) } . type_num
95
99
}
96
100
}
97
101
98
- /// Represents numpy data type.
102
+ /// Represents NumPy data type.
99
103
///
100
104
/// This is an incomplete counterpart of
101
105
/// [Enumerated Types](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types)
@@ -119,26 +123,32 @@ pub enum DataType {
119
123
}
120
124
121
125
impl DataType {
122
- /// Construct `DataType` from
123
- /// [Enumerated Types](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
126
+ /// Convert `self` into an
127
+ /// [enumerated type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
128
+ pub fn into_typenum ( self ) -> c_int {
129
+ self . into_npy_type ( ) as _
130
+ }
131
+
132
+ /// Construct the data type from an
133
+ /// [enumerated type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
124
134
pub fn from_typenum ( typenum : c_int ) -> Option < Self > {
125
135
Some ( match typenum {
126
- x if x == NPY_TYPES :: NPY_BOOL as i32 => DataType :: Bool ,
127
- x if x == NPY_TYPES :: NPY_BYTE as i32 => DataType :: Int8 ,
128
- x if x == NPY_TYPES :: NPY_SHORT as i32 => DataType :: Int16 ,
129
- x if x == NPY_TYPES :: NPY_INT as i32 => Self :: integer :: < c_int > ( ) ?,
130
- x if x == NPY_TYPES :: NPY_LONG as i32 => Self :: integer :: < c_long > ( ) ?,
131
- x if x == NPY_TYPES :: NPY_LONGLONG as i32 => Self :: integer :: < c_longlong > ( ) ?,
132
- x if x == NPY_TYPES :: NPY_UBYTE as i32 => DataType :: Uint8 ,
133
- x if x == NPY_TYPES :: NPY_USHORT as i32 => DataType :: Uint16 ,
134
- x if x == NPY_TYPES :: NPY_UINT as i32 => Self :: integer :: < c_uint > ( ) ?,
135
- x if x == NPY_TYPES :: NPY_ULONG as i32 => Self :: integer :: < c_ulong > ( ) ?,
136
- x if x == NPY_TYPES :: NPY_ULONGLONG as i32 => Self :: integer :: < c_ulonglong > ( ) ?,
137
- x if x == NPY_TYPES :: NPY_FLOAT as i32 => DataType :: Float32 ,
138
- x if x == NPY_TYPES :: NPY_DOUBLE as i32 => DataType :: Float64 ,
139
- x if x == NPY_TYPES :: NPY_CFLOAT as i32 => DataType :: Complex32 ,
140
- x if x == NPY_TYPES :: NPY_CDOUBLE as i32 => DataType :: Complex64 ,
141
- x if x == NPY_TYPES :: NPY_OBJECT as i32 => DataType :: Object ,
136
+ x if x == NPY_TYPES :: NPY_BOOL as c_int => DataType :: Bool ,
137
+ x if x == NPY_TYPES :: NPY_BYTE as c_int => DataType :: Int8 ,
138
+ x if x == NPY_TYPES :: NPY_SHORT as c_int => DataType :: Int16 ,
139
+ x if x == NPY_TYPES :: NPY_INT as c_int => Self :: integer :: < c_int > ( ) ?,
140
+ x if x == NPY_TYPES :: NPY_LONG as c_int => Self :: integer :: < c_long > ( ) ?,
141
+ x if x == NPY_TYPES :: NPY_LONGLONG as c_int => Self :: integer :: < c_longlong > ( ) ?,
142
+ x if x == NPY_TYPES :: NPY_UBYTE as c_int => DataType :: Uint8 ,
143
+ x if x == NPY_TYPES :: NPY_USHORT as c_int => DataType :: Uint16 ,
144
+ x if x == NPY_TYPES :: NPY_UINT as c_int => Self :: integer :: < c_uint > ( ) ?,
145
+ x if x == NPY_TYPES :: NPY_ULONG as c_int => Self :: integer :: < c_ulong > ( ) ?,
146
+ x if x == NPY_TYPES :: NPY_ULONGLONG as c_int => Self :: integer :: < c_ulonglong > ( ) ?,
147
+ x if x == NPY_TYPES :: NPY_FLOAT as c_int => DataType :: Float32 ,
148
+ x if x == NPY_TYPES :: NPY_DOUBLE as c_int => DataType :: Float64 ,
149
+ x if x == NPY_TYPES :: NPY_CFLOAT as c_int => DataType :: Complex32 ,
150
+ x if x == NPY_TYPES :: NPY_CDOUBLE as c_int => DataType :: Complex64 ,
151
+ x if x == NPY_TYPES :: NPY_OBJECT as c_int => DataType :: Object ,
142
152
_ => return None ,
143
153
} )
144
154
}
@@ -160,9 +170,7 @@ impl DataType {
160
170
} )
161
171
}
162
172
163
- /// Convert `self` into
164
- /// [Enumerated Types](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
165
- pub fn into_ctype ( self ) -> NPY_TYPES {
173
+ fn into_npy_type ( self ) -> NPY_TYPES {
166
174
fn npy_int_type_lookup < T , T0 , T1 , T2 > ( npy_types : [ NPY_TYPES ; 3 ] ) -> NPY_TYPES {
167
175
// `npy_common.h` defines the integer aliases. In order, it checks:
168
176
// NPY_BITSOF_LONG, NPY_BITSOF_LONGLONG, NPY_BITSOF_INT, NPY_BITSOF_SHORT, NPY_BITSOF_CHAR
@@ -284,7 +292,7 @@ macro_rules! impl_num_element {
284
292
}
285
293
286
294
fn get_dtype( py: Python ) -> & PyArrayDescr {
287
- PyArrayDescr :: from_npy_type( py, $data_type. into_ctype ( ) )
295
+ PyArrayDescr :: from_npy_type( py, $data_type. into_npy_type ( ) )
288
296
}
289
297
}
290
298
} ;
0 commit comments