Skip to content

Commit d1ca21a

Browse files
committed
Add PyArrayDescr::into_dtype_ptr()
1 parent 82e641b commit d1ca21a

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/array.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
430430
let dims = dims.into_dimension();
431431
let ptr = PY_ARRAY_API.PyArray_NewFromDescr(
432432
PY_ARRAY_API.get_type_object(npyffi::NpyTypes::PyArray_Type),
433-
T::get_dtype(py).into_ptr() as _,
433+
T::get_dtype(py).into_dtype_ptr(),
434434
dims.ndim_cint(),
435435
dims.as_dims_ptr(),
436436
strides as *mut npy_intp, // strides
@@ -454,7 +454,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
454454
let dims = dims.into_dimension();
455455
let ptr = PY_ARRAY_API.PyArray_NewFromDescr(
456456
PY_ARRAY_API.get_type_object(npyffi::NpyTypes::PyArray_Type),
457-
T::get_dtype(py).into_ptr() as _,
457+
T::get_dtype(py).into_dtype_ptr(),
458458
dims.ndim_cint(),
459459
dims.as_dims_ptr(),
460460
strides as *mut npy_intp, // strides
@@ -567,11 +567,10 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
567567
{
568568
let dims = dims.into_dimension();
569569
unsafe {
570-
let dtype = T::get_dtype(py);
571570
let ptr = PY_ARRAY_API.PyArray_Zeros(
572571
dims.ndim_cint(),
573572
dims.as_dims_ptr(),
574-
dtype.into_ptr() as _,
573+
T::get_dtype(py).into_dtype_ptr(),
575574
if is_fortran { -1 } else { 0 },
576575
);
577576
Self::from_owned_ptr(py, ptr)
@@ -1102,10 +1101,9 @@ impl<T: Element, D> PyArray<T, D> {
11021101
/// ```
11031102
pub fn cast<'py, U: Element>(&'py self, is_fortran: bool) -> PyResult<&'py PyArray<U, D>> {
11041103
let ptr = unsafe {
1105-
let dtype = U::get_dtype(self.py());
11061104
PY_ARRAY_API.PyArray_CastToType(
11071105
self.as_array_ptr(),
1108-
dtype.into_ptr() as _,
1106+
U::get_dtype(self.py()).into_dtype_ptr(),
11091107
if is_fortran { -1 } else { 0 },
11101108
)
11111109
};

src/dtype.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ impl PyArrayDescr {
4343
self.as_ptr() as _
4444
}
4545

46+
/// Returns `self` as `*mut PyArray_Descr` while increasing the reference count.
47+
///
48+
/// Useful in cases where the descriptor is stolen by the API.
49+
pub fn into_dtype_ptr(&self) -> *mut PyArray_Descr {
50+
self.into_ptr() as _
51+
}
52+
4653
/// Returns the internal `PyType` that this `dtype` holds.
4754
///
4855
/// # Example

0 commit comments

Comments
 (0)