Skip to content

Commit b54bb16

Browse files
rthkngwyu
authored andcommitted
Support converting Array1<usize> (#98)
* Support converting Array1<usize> * Review comments
1 parent 96d2140 commit b54bb16

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/types.rs

+11
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ impl_type_num!(c32, Complex32, NPY_CFLOAT);
105105
impl_type_num!(c64, Complex64, NPY_CDOUBLE);
106106
impl_type_num!(*mut PyObject, PyObject, NPY_OBJECT);
107107

108+
cfg_if! {
109+
if #[cfg(all(target_pointer_width = "64", windows))] {
110+
impl_type_num!(usize, Uint64, NPY_ULONGLONG);
111+
} else if #[cfg(all(target_pointer_width = "64", not(windows)))] {
112+
impl_type_num!(usize, Uint64, NPY_ULONG, NPY_ULONGLONG);
113+
} else if #[cfg(all(target_pointer_width = "32", windows))] {
114+
impl_type_num!(usize, Uint32, NPY_UINT, NPY_ULONG);
115+
} else if #[cfg(all(target_pointer_width = "32", not(windows)))] {
116+
impl_type_num!(usize, Uint32, NPY_UINT);
117+
}
118+
}
108119
cfg_if! {
109120
if #[cfg(any(target_pointer_width = "32", windows))] {
110121
impl_type_num!(i32, Int32, NPY_INT, NPY_LONG);

tests/array.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,25 @@ macro_rules! small_array_test {
249249
};
250250
}
251251

252-
small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64);
252+
small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64 usize);
253+
254+
#[test]
255+
fn array_usize_dtype() {
256+
let gil = pyo3::Python::acquire_gil();
257+
let py = gil.python();
258+
let locals = PyDict::new(py);
259+
260+
let a: Vec<usize> = vec![1, 2, 3];
261+
let x = a.into_pyarray(py);
262+
let x_repr = format!("{:?}", x);
263+
264+
let x_repr_expected = if cfg!(target_pointer_width = "64") {
265+
"array([1, 2, 3], dtype=uint64)"
266+
} else {
267+
"array([1, 2, 3], dtype=uint32)"
268+
};
269+
assert_eq!(x_repr, x_repr_expected);
270+
}
253271

254272
#[test]
255273
fn array_cast() {

0 commit comments

Comments
 (0)