PEP draft: Safer mutability semantics for the buffer protocol

@sgeisenh if it is useful to you I’d be happy for us to also have an issue / PR / or even an experimental feature in PyO3 if you’d want to play around with how this ends up being usable in a Rust context.

The slices can be arbitrarily complex (n-dimensional, strided…), so computing whether they overlap is a difficult problem - and potentially expensive.

You may be interested in some prior art here in the rust-numpy crate, where we have put some dynamic borrow checking into global state to try to guarantee that at least all accesses going through rust-numpy comply with Rust’s semantics. The first thread of discussion is at RFC: Add dynamic borrow checking for dereferencing NumPy arrays. by adamreichold · Pull Request #274 · PyO3/rust-numpy · GitHub

We’ve run into exactly the complexity of the kind suggested by @pitrou too; we tried to come up with a solution which is a reasonable balance between accuracy and performance, though it is narrower in scope than this PEP would be aiming to solve.

numpy::borrow - Rust (docs.rs)

2 Likes