Open
Description
Hey, I want to write some Rust algorithms that operate on a numpy array that was created in python. E.g.:
arr = np.ones((1000, 768))
dataset = MyRustWrapper(arr) # Rust wrapper class
idx = 199
dataset.get_nearest_neighbor(idx)
Because the array is quite big, I would like to avoid copying the data and just wrap a Array2<f32>
that is passed from python.
But how can I take a Array2<f32>
and pass it into the fn new
of the Rust struct? I only managed to get this working, by extenting the lifetime of new<'py>(arr: PyReadonlyArray2<'py, f32>) -> PyResult<Self>
to 'static
with unsafe which is likely incorrect:
struct MyRustWrapper(PyReadonlyArray2<'static, f32>)
impl MyRustWrapper {
fn new<'py>(arr: PyReadonlyArray2<'py, f32>) -> PyResult<Self>{
Self(unsafe { std::mem::transmute(arr) })
}
}
Also on a sidenote, how can I make sure that the rows of the numpy array are all f32
and are laid out as slices in memory?
Thanks in advance for any help.