@@ -63,14 +63,19 @@ impl PyArrayDescr {
63
63
DataType :: from_typenum ( self . get_typenum ( ) )
64
64
}
65
65
66
+ /// Shortcut for creating a descriptor of 'object' type.
67
+ pub fn object ( py : Python ) -> & Self {
68
+ Self :: from_npy_type ( py, NPY_TYPES :: NPY_OBJECT )
69
+ }
70
+
66
71
fn from_npy_type ( py : Python , npy_type : NPY_TYPES ) -> & Self {
67
72
unsafe {
68
73
let descr = PY_ARRAY_API . PyArray_DescrFromType ( npy_type as i32 ) ;
69
74
py. from_owned_ptr ( descr as _ )
70
75
}
71
76
}
72
77
73
- fn get_typenum ( & self ) -> std:: os:: raw:: c_int {
78
+ pub ( crate ) fn get_typenum ( & self ) -> std:: os:: raw:: c_int {
74
79
unsafe { * self . as_dtype_ptr ( ) } . type_num
75
80
}
76
81
}
@@ -206,6 +211,10 @@ impl DataType {
206
211
/// fn is_same_type(dtype: &PyArrayDescr) -> bool {
207
212
/// dtype.get_datatype() == Some(DataType::Object)
208
213
/// }
214
+ ///
215
+ /// fn get_dtype(py: Python) -> &PyArrayDescr {
216
+ /// PyArrayDescr::object(py)
217
+ /// }
209
218
/// }
210
219
///
211
220
/// Python::with_gil(|py| {
@@ -223,17 +232,8 @@ pub unsafe trait Element: Clone + Send {
223
232
/// Returns if the give `dtype` is convertible to `Self` in Rust.
224
233
fn is_same_type ( dtype : & PyArrayDescr ) -> bool ;
225
234
226
- /// Returns the corresponding
227
- /// [Enumerated Type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
228
- #[ inline]
229
- fn npy_type ( ) -> NPY_TYPES {
230
- Self :: DATA_TYPE . into_ctype ( )
231
- }
232
-
233
235
/// Create `dtype`.
234
- fn get_dtype ( py : Python ) -> & PyArrayDescr {
235
- PyArrayDescr :: from_npy_type ( py, Self :: npy_type ( ) )
236
- }
236
+ fn get_dtype ( py : Python ) -> & PyArrayDescr ;
237
237
}
238
238
239
239
macro_rules! impl_num_element {
@@ -243,6 +243,9 @@ macro_rules! impl_num_element {
243
243
fn is_same_type( dtype: & PyArrayDescr ) -> bool {
244
244
$( dtype. get_typenum( ) == NPY_TYPES :: $npy_types as i32 ||) + false
245
245
}
246
+ fn get_dtype( py: Python ) -> & PyArrayDescr {
247
+ PyArrayDescr :: from_npy_type( py, DataType :: $npy_dat_t. into_ctype( ) )
248
+ }
246
249
}
247
250
} ;
248
251
}
@@ -287,4 +290,7 @@ unsafe impl Element for PyObject {
287
290
fn is_same_type ( dtype : & PyArrayDescr ) -> bool {
288
291
dtype. get_typenum ( ) == NPY_TYPES :: NPY_OBJECT as i32
289
292
}
293
+ fn get_dtype ( py : Python ) -> & PyArrayDescr {
294
+ PyArrayDescr :: object ( py)
295
+ }
290
296
}
0 commit comments