PyO3 / rust-numpy

PyO3-based Rust bindings of the NumPy C-API
BSD 2-Clause "Simplified" License
1.07k stars 98 forks source link

How create Array from exists Memory without copy? #425

Open ZHUI opened 3 months ago

ZHUI commented 3 months ago

I find https://docs.rs/numpy/latest/numpy/array/struct.PyArray.html#method.from_slice

by it copy the memory.

adamreichold commented 3 months ago

Conversions which take ownership of Rust data and transfer it over to Python use the IntoPyArray trait.

It basically supports Vec<T>, Box<[T]> and Array<T, D> where T is any of the supported element types.

If this ties in with https://github.com/PyO3/pyo3/issues/4058, I could image adding support for Mmap reinterpreted as [T] to turn this into a one-dimensional NumPy array without copying which you can then reshape into the final form without copying via the usual NumPy API.