diff --git a/src/types.rs b/src/types.rs index d46945678..7941bfb8a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -105,6 +105,17 @@ impl_type_num!(c32, Complex32, NPY_CFLOAT); impl_type_num!(c64, Complex64, NPY_CDOUBLE); impl_type_num!(*mut PyObject, PyObject, NPY_OBJECT); +cfg_if! { + if #[cfg(all(target_pointer_width = "64", windows))] { + impl_type_num!(usize, Uint64, NPY_ULONGLONG); + } else if #[cfg(all(target_pointer_width = "64", not(windows)))] { + impl_type_num!(usize, Uint64, NPY_ULONG, NPY_ULONGLONG); + } else if #[cfg(all(target_pointer_width = "32", windows))] { + impl_type_num!(usize, Uint32, NPY_UINT, NPY_ULONG); + } else if #[cfg(all(target_pointer_width = "32", not(windows)))] { + impl_type_num!(usize, Uint32, NPY_UINT); + } +} cfg_if! { if #[cfg(any(target_pointer_width = "32", windows))] { impl_type_num!(i32, Int32, NPY_INT, NPY_LONG); diff --git a/tests/array.rs b/tests/array.rs index 5c273c9e2..0951a30dc 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -249,7 +249,25 @@ macro_rules! small_array_test { }; } -small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64); +small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64 usize); + +#[test] +fn array_usize_dtype() { + let gil = pyo3::Python::acquire_gil(); + let py = gil.python(); + let locals = PyDict::new(py); + + let a: Vec = vec![1, 2, 3]; + let x = a.into_pyarray(py); + let x_repr = format!("{:?}", x); + + let x_repr_expected = if cfg!(target_pointer_width = "64") { + "array([1, 2, 3], dtype=uint64)" + } else { + "array([1, 2, 3], dtype=uint32)" + }; + assert_eq!(x_repr, x_repr_expected); +} #[test] fn array_cast() {