PyO3 / rust-numpy

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

Borrow checking should use a capsule API #359

Closed adamreichold closed 1 year ago

adamreichold commented 1 year ago

The dynamic borrow checking currently uses Rust static global data to store the currently active borrows which means that it is limited to checking the borrows by a single statically linked extension but not e.g. across two separate extensions using rust-numpy.

I think the best way to mitigate this is to use a capsule API providing a C API/ABI for the basic borrow checking operations with the global state being attached to a Python module, e.g. an attribute named _BORROW_CHECK_API injected into the numpy.core.multiarray module. This would enable both multiple extensions using rust-numpy to share borrow checking data as well as extensions written in other languages to participate in borrow checking.