Skip to content

Commit 32740b3

Browse files
committed
Add documentation on the consequences of NumPy matching C's integeer hierarchy.
1 parent 74a32b4 commit 32740b3

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/dtype.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@ impl PyArrayDescr {
368368
/// Currently, only integer/float/complex/object types are supported. The [NumPy documentation][enumerated-types]
369369
/// list the other built-in types which we are not yet implemented.
370370
///
371+
/// Note that NumPy's integer types like `numpy.int_` and `numpy.uint` are based on C's integer hierarchy
372+
/// which implies that their widths change depending on the platform's [data model][data-models].
373+
/// For example, `numpy.int_` matches C's `long` which is 32 bits wide on Windows (using the LLP64 data model)
374+
/// but 64 bits wide on Linux (using the LP64 data model).
375+
///
376+
/// In contrast, Rust's [`isize`] and [`usize`] types are defined to have the same width as a pointer
377+
/// and are therefore always 64 bits wide on 64-bit platforms. If you want to match NumPy's behaviour,
378+
/// consider using the [`c_long`][std::ffi::c_long] and [`c_ulong`][std::ffi::c_ulong] type aliases.
379+
///
371380
/// # Safety
372381
///
373382
/// A type `T` that implements this trait should be safe when managed by a NumPy
@@ -390,6 +399,7 @@ impl PyArrayDescr {
390399
/// safely and efficiently using [`from_owned_object_array`][crate::PyArray::from_owned_object_array].
391400
///
392401
/// [enumerated-types]: https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types
402+
/// [data-models]: https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models
393403
pub unsafe trait Element: Clone + Send {
394404
/// Flag that indicates whether this type is trivially copyable.
395405
///

0 commit comments

Comments
 (0)