ndarray
ndarray copied to clipboard
Add offset_to_first_elem method for Array
This adds a method to get the offset from the start of the allocation to the first element. Without this method, correctly reinterpreting the results of .into_raw_vec()
as an n-D array is tricky.
I came across a bug in the numpy
crate (PyO3/rust-numpy#182) due to not handling this properly, so the the primary purpose of this PR is to help properly fix that bug.
The zero-sized-element case is a bit weird, because for many use cases, a return value of Some(0)
would be fine. However, if we want the user to be able to compute in-bounds indices into the Vec
using the offset, shape, and strides, we need to compute an offset that will work based on the shape and strides.
I've fixed/suppressed a couple of clippy warnings unrelated to offset_to_first_elem
which have appeared with the new release of Rust.
By the way, is there a better name for this method?
While it's a breaking change, we wait with merging this until the 0.15.x releases are done