ndarray icon indicating copy to clipboard operation
ndarray copied to clipboard

Add offset_to_first_elem method for Array

Open jturner314 opened this issue 3 years ago • 1 comments

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?

jturner314 avatar May 09 '21 04:05 jturner314

While it's a breaking change, we wait with merging this until the 0.15.x releases are done

bluss avatar May 09 '21 19:05 bluss