Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended way for creating a wrapper class in Rust #426

Open
tadeohepperle opened this issue Apr 12, 2024 · 2 comments
Open

Recommended way for creating a wrapper class in Rust #426

tadeohepperle opened this issue Apr 12, 2024 · 2 comments
Labels

Comments

@tadeohepperle
Copy link

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.

@tadeohepperle tadeohepperle changed the title Recommended way of creating a wrapper class that takes ownership of a numpy array created in Python Recommended way for creating a wrapper class in Rust Apr 12, 2024
@adamreichold
Copy link
Member

You can detach the by taking it in the form Py<PyArray2<f32>> which you can stored inside your Rust type. Cloning/copying this value will only increment its reference count, not duplicate the data.

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?

If you take PyArray2<T> then it is ensured that it contains values of type T, e.g. f32 as in your example. Such an array will not store values of dynamic type for which only PyArray2<PyObject> would be suitable.

You can whether an array is laid contiguously in memory using the is_contiguous method and access it as a slice using the as_slice(_mut) method.

@tadeohepperle
Copy link
Author

@adamreichold Thanks a lot for your help, that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants