Skip to content

Commit 6fbbec1

Browse files
committed
Perform borrow checking through a capsule-based API
This places the global state required for dynamic borrow checking of NumPy arrays into a capsule exposed as the numpy.core.multiarray._BORROW_CHECKING_API attribute. This has two benefits: First, all extensions built using rust-numpy will share this global state and hence cooperate in borrow checking. Second, in the future, other libraries that want to borrow check their access to NumPy array can cooperate using this C-compatible API. This does not checking the borrow checking implementation at all, but instead of accessing Rust static data, all accesses are funneled through the above-mentioned capsule API with access to it being cached for performance reasons as we already do for the _ARRAY_API and _UFUNC_API capsules. This means that eventually, the implementation of the borrow checking API could be different from the one the current extension would provide with the adaptors to the C API taking of any differences in e.g. the definition of `BorrowKey`. For now, they will of course usually be the same and this change just adds a huge amount of boiler plate to go from Rust to C-compatible back to Rust.
1 parent 37238e7 commit 6fbbec1

File tree

2 files changed

+326
-64
lines changed

2 files changed

+326
-64
lines changed

src/borrow/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ use crate::dtype::Element;
179179
use crate::error::{BorrowError, NotContiguousError};
180180
use crate::npyffi::{self, PyArrayObject, NPY_ARRAY_WRITEABLE};
181181

182-
use shared::{BorrowKey, BORROW_FLAGS};
182+
use shared::{acquire, acquire_mut, release, release_mut, BorrowKey};
183183

184184
/// Read-only borrow of an array.
185185
///
@@ -247,7 +247,7 @@ where
247247
let address = base_address(array);
248248
let key = borrow_key(array);
249249

250-
BORROW_FLAGS.acquire(array.py(), address, key)?;
250+
acquire(array.py(), address, key)?;
251251

252252
Ok(Self {
253253
array,
@@ -339,9 +339,7 @@ where
339339
D: Dimension,
340340
{
341341
fn clone(&self) -> Self {
342-
BORROW_FLAGS
343-
.acquire(self.array.py(), self.address, self.key)
344-
.unwrap();
342+
acquire(self.array.py(), self.address, self.key).unwrap();
345343

346344
Self {
347345
array: self.array,
@@ -357,7 +355,7 @@ where
357355
D: Dimension,
358356
{
359357
fn drop(&mut self) {
360-
BORROW_FLAGS.release(self.array.py(), self.address, self.key);
358+
release(self.array.py(), self.address, self.key);
361359
}
362360
}
363361

@@ -448,7 +446,7 @@ where
448446
let address = base_address(array);
449447
let key = borrow_key(array);
450448

451-
BORROW_FLAGS.acquire_mut(array.py(), address, key)?;
449+
acquire_mut(array.py(), address, key)?;
452450

453451
Ok(Self {
454452
array,
@@ -581,7 +579,7 @@ where
581579
D: Dimension,
582580
{
583581
fn drop(&mut self) {
584-
BORROW_FLAGS.release_mut(self.array.py(), self.address, self.key);
582+
release_mut(self.array.py(), self.address, self.key);
585583
}
586584
}
587585

0 commit comments

Comments
 (0)