@@ -21,7 +21,7 @@ pub use num_complex::{Complex32, Complex64};
21
21
/// .unwrap()
22
22
/// .downcast()
23
23
/// .unwrap();
24
- /// assert_eq !(dtype.get_datatype().unwrap(), numpy::DataType::Float64 );
24
+ /// assert !(dtype.is_equiv_to( numpy::PyArrayDescr::of::<f64>(py)) );
25
25
/// });
26
26
/// ```
27
27
pub struct PyArrayDescr ( PyAny ) ;
@@ -68,11 +68,6 @@ impl PyArrayDescr {
68
68
unsafe { PyType :: from_type_ptr ( self . py ( ) , dtype_type_ptr) }
69
69
}
70
70
71
- /// Returns the data type as `DataType` enum.
72
- pub fn get_datatype ( & self ) -> Option < DataType > {
73
- DataType :: from_typenum ( self . get_typenum ( ) )
74
- }
75
-
76
71
/// Shortcut for creating a descriptor of 'object' type.
77
72
pub fn object ( py : Python ) -> & Self {
78
73
Self :: from_npy_type ( py, NPY_TYPES :: NPY_OBJECT )
@@ -103,125 +98,6 @@ impl PyArrayDescr {
103
98
}
104
99
}
105
100
106
- /// Represents NumPy data type.
107
- ///
108
- /// This is an incomplete counterpart of
109
- /// [Enumerated Types](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types)
110
- /// in numpy C-API.
111
- #[ derive( Clone , Debug , Eq , PartialEq ) ]
112
- pub enum DataType {
113
- Bool ,
114
- Int8 ,
115
- Int16 ,
116
- Int32 ,
117
- Int64 ,
118
- Uint8 ,
119
- Uint16 ,
120
- Uint32 ,
121
- Uint64 ,
122
- Float32 ,
123
- Float64 ,
124
- Complex32 ,
125
- Complex64 ,
126
- Object ,
127
- }
128
-
129
- impl DataType {
130
- /// Convert `self` into an
131
- /// [enumerated type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
132
- pub fn into_typenum ( self ) -> c_int {
133
- self . into_npy_type ( ) as _
134
- }
135
-
136
- /// Construct the data type from an
137
- /// [enumerated type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
138
- pub fn from_typenum ( typenum : c_int ) -> Option < Self > {
139
- Some ( match typenum {
140
- x if x == NPY_TYPES :: NPY_BOOL as c_int => DataType :: Bool ,
141
- x if x == NPY_TYPES :: NPY_BYTE as c_int => DataType :: Int8 ,
142
- x if x == NPY_TYPES :: NPY_SHORT as c_int => DataType :: Int16 ,
143
- x if x == NPY_TYPES :: NPY_INT as c_int => Self :: integer :: < c_int > ( ) ?,
144
- x if x == NPY_TYPES :: NPY_LONG as c_int => Self :: integer :: < c_long > ( ) ?,
145
- x if x == NPY_TYPES :: NPY_LONGLONG as c_int => Self :: integer :: < c_longlong > ( ) ?,
146
- x if x == NPY_TYPES :: NPY_UBYTE as c_int => DataType :: Uint8 ,
147
- x if x == NPY_TYPES :: NPY_USHORT as c_int => DataType :: Uint16 ,
148
- x if x == NPY_TYPES :: NPY_UINT as c_int => Self :: integer :: < c_uint > ( ) ?,
149
- x if x == NPY_TYPES :: NPY_ULONG as c_int => Self :: integer :: < c_ulong > ( ) ?,
150
- x if x == NPY_TYPES :: NPY_ULONGLONG as c_int => Self :: integer :: < c_ulonglong > ( ) ?,
151
- x if x == NPY_TYPES :: NPY_FLOAT as c_int => DataType :: Float32 ,
152
- x if x == NPY_TYPES :: NPY_DOUBLE as c_int => DataType :: Float64 ,
153
- x if x == NPY_TYPES :: NPY_CFLOAT as c_int => DataType :: Complex32 ,
154
- x if x == NPY_TYPES :: NPY_CDOUBLE as c_int => DataType :: Complex64 ,
155
- x if x == NPY_TYPES :: NPY_OBJECT as c_int => DataType :: Object ,
156
- _ => return None ,
157
- } )
158
- }
159
-
160
- #[ inline]
161
- fn integer < T : Bounded + Zero + Sized + PartialEq > ( ) -> Option < Self > {
162
- let is_unsigned = T :: min_value ( ) == T :: zero ( ) ;
163
- let bit_width = size_of :: < T > ( ) << 3 ;
164
- Some ( match ( is_unsigned, bit_width) {
165
- ( false , 8 ) => Self :: Int8 ,
166
- ( false , 16 ) => Self :: Int16 ,
167
- ( false , 32 ) => Self :: Int32 ,
168
- ( false , 64 ) => Self :: Int64 ,
169
- ( true , 8 ) => Self :: Uint8 ,
170
- ( true , 16 ) => Self :: Uint16 ,
171
- ( true , 32 ) => Self :: Uint32 ,
172
- ( true , 64 ) => Self :: Uint64 ,
173
- _ => return None ,
174
- } )
175
- }
176
-
177
- fn into_npy_type ( self ) -> NPY_TYPES {
178
- fn npy_int_type_lookup < T , T0 , T1 , T2 > ( npy_types : [ NPY_TYPES ; 3 ] ) -> NPY_TYPES {
179
- // `npy_common.h` defines the integer aliases. In order, it checks:
180
- // NPY_BITSOF_LONG, NPY_BITSOF_LONGLONG, NPY_BITSOF_INT, NPY_BITSOF_SHORT, NPY_BITSOF_CHAR
181
- // and assigns the alias to the first matching size, so we should check in this order.
182
- match size_of :: < T > ( ) {
183
- x if x == size_of :: < T0 > ( ) => npy_types[ 0 ] ,
184
- x if x == size_of :: < T1 > ( ) => npy_types[ 1 ] ,
185
- x if x == size_of :: < T2 > ( ) => npy_types[ 2 ] ,
186
- _ => panic ! ( "Unable to match integer type descriptor: {:?}" , npy_types) ,
187
- }
188
- }
189
-
190
- match self {
191
- DataType :: Bool => NPY_TYPES :: NPY_BOOL ,
192
- DataType :: Int8 => NPY_TYPES :: NPY_BYTE ,
193
- DataType :: Int16 => NPY_TYPES :: NPY_SHORT ,
194
- DataType :: Int32 => npy_int_type_lookup :: < i32 , c_long , c_int , c_short > ( [
195
- NPY_TYPES :: NPY_LONG ,
196
- NPY_TYPES :: NPY_INT ,
197
- NPY_TYPES :: NPY_SHORT ,
198
- ] ) ,
199
- DataType :: Int64 => npy_int_type_lookup :: < i64 , c_long , c_longlong , c_int > ( [
200
- NPY_TYPES :: NPY_LONG ,
201
- NPY_TYPES :: NPY_LONGLONG ,
202
- NPY_TYPES :: NPY_INT ,
203
- ] ) ,
204
- DataType :: Uint8 => NPY_TYPES :: NPY_UBYTE ,
205
- DataType :: Uint16 => NPY_TYPES :: NPY_USHORT ,
206
- DataType :: Uint32 => npy_int_type_lookup :: < u32 , c_ulong , c_uint , c_ushort > ( [
207
- NPY_TYPES :: NPY_ULONG ,
208
- NPY_TYPES :: NPY_UINT ,
209
- NPY_TYPES :: NPY_USHORT ,
210
- ] ) ,
211
- DataType :: Uint64 => npy_int_type_lookup :: < u64 , c_ulong , c_ulonglong , c_uint > ( [
212
- NPY_TYPES :: NPY_ULONG ,
213
- NPY_TYPES :: NPY_ULONGLONG ,
214
- NPY_TYPES :: NPY_UINT ,
215
- ] ) ,
216
- DataType :: Float32 => NPY_TYPES :: NPY_FLOAT ,
217
- DataType :: Float64 => NPY_TYPES :: NPY_DOUBLE ,
218
- DataType :: Complex32 => NPY_TYPES :: NPY_CFLOAT ,
219
- DataType :: Complex64 => NPY_TYPES :: NPY_CDOUBLE ,
220
- DataType :: Object => NPY_TYPES :: NPY_OBJECT ,
221
- }
222
- }
223
- }
224
-
225
101
/// Represents that a type can be an element of `PyArray`.
226
102
///
227
103
/// Currently, only integer/float/complex types are supported.
@@ -243,7 +119,7 @@ impl DataType {
243
119
/// on Python's heap using PyO3's [Py](pyo3::Py) type.
244
120
///
245
121
/// ```
246
- /// use numpy::{ndarray::Array2, DataType, Element, PyArray, PyArrayDescr, ToPyArray};
122
+ /// use numpy::{ndarray::Array2, Element, PyArray, PyArrayDescr, ToPyArray};
247
123
/// use pyo3::{pyclass, Py, Python};
248
124
///
249
125
/// #[pyclass]
0 commit comments