Skip to content

Commit c16fbb1

Browse files
committed
Do not elide lifetimes and try to be disciplined about naming the GIL lifetime 'py.
1 parent 652619d commit c16fbb1

File tree

19 files changed

+120
-101
lines changed

19 files changed

+120
-101
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn};
4848
use pyo3::{pymodule, types::PyModule, PyResult, Python};
4949

5050
#[pymodule]
51-
fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
51+
fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
5252
// example using immutable borrows producing a new array
5353
fn axpy(a: f64, x: ArrayViewD<'_, f64>, y: ArrayViewD<'_, f64>) -> ArrayD<f64> {
5454
a * &x + &y
@@ -65,8 +65,8 @@ fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
6565
fn axpy_py<'py>(
6666
py: Python<'py>,
6767
a: f64,
68-
x: PyReadonlyArrayDyn<f64>,
69-
y: PyReadonlyArrayDyn<f64>,
68+
x: PyReadonlyArrayDyn<'py, f64>,
69+
y: PyReadonlyArrayDyn<'py, f64>,
7070
) -> &'py PyArrayDyn<f64> {
7171
let x = x.as_array();
7272
let y = y.as_array();
@@ -77,7 +77,7 @@ fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
7777
// wrapper of `mult`
7878
#[pyfn(m)]
7979
#[pyo3(name = "mult")]
80-
fn mult_py(_py: Python<'_>, a: f64, x: &PyArrayDyn<f64>) {
80+
fn mult_py<'py>(a: f64, x: &'py PyArrayDyn<f64>) {
8181
let x = unsafe { x.as_array_mut() };
8282
mult(a, x);
8383
}

examples/linalg/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2};
33
use pyo3::{exceptions::PyRuntimeError, pymodule, types::PyModule, PyResult, Python};
44

55
#[pymodule]
6-
fn rust_linalg(_py: Python, m: &PyModule) -> PyResult<()> {
6+
fn rust_linalg<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
77
#[pyfn(m)]
8-
fn inv<'py>(py: Python<'py>, x: PyReadonlyArray2<f64>) -> PyResult<&'py PyArray2<f64>> {
8+
fn inv<'py>(py: Python<'py>, x: PyReadonlyArray2<'py, f64>) -> PyResult<&'py PyArray2<f64>> {
99
let x = x.as_array();
1010
let y = x
1111
.inv()

examples/parallel/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1, PyReadonlyArray2};
66
use pyo3::{pymodule, types::PyModule, PyResult, Python};
77

88
#[pymodule]
9-
fn rust_parallel(_py: Python, m: &PyModule) -> PyResult<()> {
9+
fn rust_parallel<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
1010
#[pyfn(m)]
1111
fn rows_dot<'py>(
1212
py: Python<'py>,
13-
x: PyReadonlyArray2<f64>,
14-
y: PyReadonlyArray1<f64>,
13+
x: PyReadonlyArray2<'py, f64>,
14+
y: PyReadonlyArray1<'py, f64>,
1515
) -> &'py PyArray1<f64> {
1616
let x = x.as_array();
1717
let y = y.as_array();

examples/simple/src/lib.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,41 @@ use pyo3::{
1414
};
1515

1616
#[pymodule]
17-
fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
17+
fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
1818
// example using generic PyObject
19-
fn head(x: ArrayViewD<PyObject>) -> PyResult<PyObject> {
19+
fn head(x: ArrayViewD<'_, PyObject>) -> PyResult<PyObject> {
2020
x.get(0)
2121
.cloned()
2222
.ok_or_else(|| PyIndexError::new_err("array index out of range"))
2323
}
2424

2525
// example using immutable borrows producing a new array
26-
fn axpy(a: f64, x: ArrayViewD<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
26+
fn axpy(a: f64, x: ArrayViewD<'_, f64>, y: ArrayViewD<'_, f64>) -> ArrayD<f64> {
2727
a * &x + &y
2828
}
2929

3030
// example using a mutable borrow to modify an array in-place
31-
fn mult(a: f64, mut x: ArrayViewMutD<f64>) {
31+
fn mult(a: f64, mut x: ArrayViewMutD<'_, f64>) {
3232
x *= a;
3333
}
3434

3535
// example using complex numbers
36-
fn conj(x: ArrayViewD<Complex64>) -> ArrayD<Complex64> {
36+
fn conj(x: ArrayViewD<'_, Complex64>) -> ArrayD<Complex64> {
3737
x.map(|c| c.conj())
3838
}
3939

4040
// example using generics
41-
fn generic_add<T: Copy + Add<Output = T>>(x: ArrayView1<T>, y: ArrayView1<T>) -> Array1<T> {
41+
fn generic_add<T: Copy + Add<Output = T>>(
42+
x: ArrayView1<'_, T>,
43+
y: ArrayView1<'_, T>,
44+
) -> Array1<T> {
4245
&x + &y
4346
}
4447

4548
// wrapper of `head`
4649
#[pyfn(m)]
4750
#[pyo3(name = "head")]
48-
fn head_py(_py: Python, x: PyReadonlyArrayDyn<PyObject>) -> PyResult<PyObject> {
51+
fn head_py<'py>(x: PyReadonlyArrayDyn<'py, PyObject>) -> PyResult<PyObject> {
4952
head(x.as_array())
5053
}
5154

@@ -55,8 +58,8 @@ fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
5558
fn axpy_py<'py>(
5659
py: Python<'py>,
5760
a: f64,
58-
x: PyReadonlyArrayDyn<f64>,
59-
y: PyReadonlyArrayDyn<f64>,
61+
x: PyReadonlyArrayDyn<'py, f64>,
62+
y: PyReadonlyArrayDyn<'py, f64>,
6063
) -> &'py PyArrayDyn<f64> {
6164
let x = x.as_array();
6265
let y = y.as_array();
@@ -67,7 +70,7 @@ fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
6770
// wrapper of `mult`
6871
#[pyfn(m)]
6972
#[pyo3(name = "mult")]
70-
fn mult_py(a: f64, mut x: PyReadwriteArrayDyn<f64>) {
73+
fn mult_py<'py>(a: f64, mut x: PyReadwriteArrayDyn<'py, f64>) {
7174
let x = x.as_array_mut();
7275
mult(a, x);
7376
}
@@ -77,7 +80,7 @@ fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
7780
#[pyo3(name = "conj")]
7881
fn conj_py<'py>(
7982
py: Python<'py>,
80-
x: PyReadonlyArrayDyn<Complex64>,
83+
x: PyReadonlyArrayDyn<'py, Complex64>,
8184
) -> &'py PyArrayDyn<Complex64> {
8285
conj(x.as_array()).into_pyarray(py)
8386
}
@@ -96,9 +99,9 @@ fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
9699

97100
// example using timedelta64 array
98101
#[pyfn(m)]
99-
fn add_minutes_to_seconds(
100-
mut x: PyReadwriteArray1<Timedelta<units::Seconds>>,
101-
y: PyReadonlyArray1<Timedelta<units::Minutes>>,
102+
fn add_minutes_to_seconds<'py>(
103+
mut x: PyReadwriteArray1<'py, Timedelta<units::Seconds>>,
104+
y: PyReadonlyArray1<'py, Timedelta<units::Minutes>>,
102105
) {
103106
#[allow(deprecated)]
104107
Zip::from(x.as_array_mut())

src/array.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub type PyArray6<T> = PyArray<T, Ix6>;
118118
pub type PyArrayDyn<T> = PyArray<T, IxDyn>;
119119

120120
/// Returns a handle to NumPy's multiarray module.
121-
pub fn get_array_module(py: Python<'_>) -> PyResult<&PyModule> {
121+
pub fn get_array_module<'py>(py: Python<'py>) -> PyResult<&PyModule> {
122122
PyModule::import(py, npyffi::array::MOD_NAME)
123123
}
124124

@@ -128,7 +128,7 @@ unsafe impl<T: Element, D: Dimension> PyTypeInfo for PyArray<T, D> {
128128
const NAME: &'static str = "PyArray<T, D>";
129129
const MODULE: Option<&'static str> = Some("numpy");
130130

131-
fn type_object_raw(py: Python) -> *mut ffi::PyTypeObject {
131+
fn type_object_raw<'py>(py: Python<'py>) -> *mut ffi::PyTypeObject {
132132
unsafe { npyffi::PY_ARRAY_API.get_type_object(py, npyffi::NpyTypes::PyArray_Type) }
133133
}
134134

@@ -164,7 +164,7 @@ impl<T, D> AsPyPointer for PyArray<T, D> {
164164

165165
impl<T, D> IntoPy<Py<PyArray<T, D>>> for &'_ PyArray<T, D> {
166166
#[inline]
167-
fn into_py(self, py: Python<'_>) -> Py<PyArray<T, D>> {
167+
fn into_py<'py>(self, py: Python<'py>) -> Py<PyArray<T, D>> {
168168
unsafe { Py::from_borrowed_ptr(py, self.as_ptr()) }
169169
}
170170
}
@@ -183,7 +183,7 @@ impl<'a, T, D> From<&'a PyArray<T, D>> for &'a PyAny {
183183
}
184184

185185
impl<T, D> IntoPy<PyObject> for PyArray<T, D> {
186-
fn into_py(self, py: Python<'_>) -> PyObject {
186+
fn into_py<'py>(self, py: Python<'py>) -> PyObject {
187187
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
188188
}
189189
}
@@ -327,16 +327,16 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
327327
/// assert_eq!(arr.shape(), &[4, 5, 6]);
328328
/// });
329329
/// ```
330-
pub unsafe fn new<ID>(py: Python, dims: ID, is_fortran: bool) -> &Self
330+
pub unsafe fn new<'py, ID>(py: Python<'py>, dims: ID, is_fortran: bool) -> &Self
331331
where
332332
ID: IntoDimension<Dim = D>,
333333
{
334334
let flags = c_int::from(is_fortran);
335335
Self::new_uninit(py, dims, ptr::null_mut(), flags)
336336
}
337337

338-
pub(crate) unsafe fn new_uninit<ID>(
339-
py: Python,
338+
pub(crate) unsafe fn new_uninit<'py, ID>(
339+
py: Python<'py>,
340340
dims: ID,
341341
strides: *const npy_intp,
342342
flag: c_int,
@@ -484,7 +484,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
484484
///
485485
/// [numpy-zeros]: https://numpy.org/doc/stable/reference/generated/numpy.zeros.html
486486
/// [PyArray_Zeros]: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Zeros
487-
pub fn zeros<ID>(py: Python, dims: ID, is_fortran: bool) -> &Self
487+
pub fn zeros<'py, ID>(py: Python<'py>, dims: ID, is_fortran: bool) -> &Self
488488
where
489489
ID: IntoDimension<Dim = D>,
490490
{
@@ -989,7 +989,7 @@ where
989989
#[doc(alias = "nalgebra")]
990990
pub unsafe fn try_as_matrix<R, C, RStride, CStride>(
991991
&self,
992-
) -> Option<nalgebra::MatrixView<N, R, C, RStride, CStride>>
992+
) -> Option<nalgebra::MatrixView<'_, N, R, C, RStride, CStride>>
993993
where
994994
R: nalgebra::Dim,
995995
C: nalgebra::Dim,
@@ -1011,7 +1011,7 @@ where
10111011
#[doc(alias = "nalgebra")]
10121012
pub unsafe fn try_as_matrix_mut<R, C, RStride, CStride>(
10131013
&self,
1014-
) -> Option<nalgebra::MatrixViewMut<N, R, C, RStride, CStride>>
1014+
) -> Option<nalgebra::MatrixViewMut<'_, N, R, C, RStride, CStride>>
10151015
where
10161016
R: nalgebra::Dim,
10171017
C: nalgebra::Dim,
@@ -1086,7 +1086,7 @@ impl<T: Copy + Element> PyArray<T, Ix0> {
10861086
}
10871087

10881088
impl<T: Element> PyArray<T, Ix1> {
1089-
/// Construct a one-dimensional array from a [slice][std::slice].
1089+
/// Construct a one-dimensional array from a [mod@slice].
10901090
///
10911091
/// # Example
10921092
///
@@ -1144,7 +1144,7 @@ impl<T: Element> PyArray<T, Ix1> {
11441144
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[97, 98, 99, 100, 101]);
11451145
/// });
11461146
/// ```
1147-
pub fn from_iter<I>(py: Python<'_>, iter: I) -> &Self
1147+
pub fn from_iter<'py, I>(py: Python<'py>, iter: I) -> &'py Self
11481148
where
11491149
I: IntoIterator<Item = T>,
11501150
{
@@ -1448,7 +1448,7 @@ impl<T: Element + AsPrimitive<f64>> PyArray<T, Ix1> {
14481448
///
14491449
/// [numpy.arange]: https://numpy.org/doc/stable/reference/generated/numpy.arange.html
14501450
/// [PyArray_Arange]: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Arange
1451-
pub fn arange(py: Python, start: T, stop: T, step: T) -> &Self {
1451+
pub fn arange<'py>(py: Python<'py>, start: T, stop: T, step: T) -> &Self {
14521452
unsafe {
14531453
let ptr = PY_ARRAY_API.PyArray_Arange(
14541454
py,

src/borrow/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ where
246246

247247
/// Provides an immutable array view of the interior of the NumPy array.
248248
#[inline(always)]
249-
pub fn as_array(&self) -> ArrayView<T, D> {
249+
pub fn as_array(&self) -> ArrayView<'_, T, D> {
250250
// SAFETY: Global borrow flags ensure aliasing discipline.
251251
unsafe { self.array.as_array() }
252252
}
@@ -278,7 +278,7 @@ where
278278
#[doc(alias = "nalgebra")]
279279
pub fn try_as_matrix<R, C, RStride, CStride>(
280280
&self,
281-
) -> Option<nalgebra::MatrixView<N, R, C, RStride, CStride>>
281+
) -> Option<nalgebra::MatrixView<'_, N, R, C, RStride, CStride>>
282282
where
283283
R: nalgebra::Dim,
284284
C: nalgebra::Dim,
@@ -300,7 +300,7 @@ where
300300
///
301301
/// Panics if the array has negative strides.
302302
#[doc(alias = "nalgebra")]
303-
pub fn as_matrix(&self) -> nalgebra::DMatrixView<N, nalgebra::Dyn, nalgebra::Dyn> {
303+
pub fn as_matrix(&self) -> nalgebra::DMatrixView<'_, N, nalgebra::Dyn, nalgebra::Dyn> {
304304
self.try_as_matrix().unwrap()
305305
}
306306
}
@@ -316,7 +316,7 @@ where
316316
///
317317
/// Panics if the array has negative strides.
318318
#[doc(alias = "nalgebra")]
319-
pub fn as_matrix(&self) -> nalgebra::DMatrixView<N, nalgebra::Dyn, nalgebra::Dyn> {
319+
pub fn as_matrix(&self) -> nalgebra::DMatrixView<'_, N, nalgebra::Dyn, nalgebra::Dyn> {
320320
self.try_as_matrix().unwrap()
321321
}
322322
}
@@ -428,7 +428,7 @@ where
428428

429429
/// Provides a mutable array view of the interior of the NumPy array.
430430
#[inline(always)]
431-
pub fn as_array_mut(&mut self) -> ArrayViewMut<T, D> {
431+
pub fn as_array_mut(&mut self) -> ArrayViewMut<'_, T, D> {
432432
// SAFETY: Global borrow flags ensure aliasing discipline.
433433
unsafe { self.array.as_array_mut() }
434434
}
@@ -460,7 +460,7 @@ where
460460
#[doc(alias = "nalgebra")]
461461
pub fn try_as_matrix_mut<R, C, RStride, CStride>(
462462
&self,
463-
) -> Option<nalgebra::MatrixViewMut<N, R, C, RStride, CStride>>
463+
) -> Option<nalgebra::MatrixViewMut<'_, N, R, C, RStride, CStride>>
464464
where
465465
R: nalgebra::Dim,
466466
C: nalgebra::Dim,
@@ -482,7 +482,7 @@ where
482482
///
483483
/// Panics if the array has negative strides.
484484
#[doc(alias = "nalgebra")]
485-
pub fn as_matrix_mut(&self) -> nalgebra::DMatrixViewMut<N, nalgebra::Dyn, nalgebra::Dyn> {
485+
pub fn as_matrix_mut(&self) -> nalgebra::DMatrixViewMut<'_, N, nalgebra::Dyn, nalgebra::Dyn> {
486486
self.try_as_matrix_mut().unwrap()
487487
}
488488
}
@@ -498,7 +498,7 @@ where
498498
///
499499
/// Panics if the array has negative strides.
500500
#[doc(alias = "nalgebra")]
501-
pub fn as_matrix_mut(&self) -> nalgebra::DMatrixViewMut<N, nalgebra::Dyn, nalgebra::Dyn> {
501+
pub fn as_matrix_mut(&self) -> nalgebra::DMatrixViewMut<'_, N, nalgebra::Dyn, nalgebra::Dyn> {
502502
self.try_as_matrix_mut().unwrap()
503503
}
504504
}

src/borrow/shared.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn get_or_insert_shared<'py>(py: Python<'py>) -> PyResult<&'py Shared> {
121121
// immediately initialize the cache used access it from this extension.
122122

123123
#[cold]
124-
fn insert_shared(py: Python) -> PyResult<*const Shared> {
124+
fn insert_shared<'py>(py: Python<'py>) -> PyResult<*const Shared> {
125125
let module = get_array_module(py)?;
126126

127127
let capsule: &PyCapsule = match module.getattr("_RUST_NUMPY_BORROW_CHECKING_API") {
@@ -170,7 +170,7 @@ fn insert_shared(py: Python) -> PyResult<*const Shared> {
170170

171171
// These entry points will be used to access the shared borrow checking API from this extension:
172172

173-
pub fn acquire(py: Python, array: *mut PyArrayObject) -> Result<(), BorrowError> {
173+
pub fn acquire<'py>(py: Python<'py>, array: *mut PyArrayObject) -> Result<(), BorrowError> {
174174
let shared = get_or_insert_shared(py).expect("Interal borrow checking API error");
175175

176176
let rc = unsafe { (shared.acquire)(shared.flags, array) };
@@ -182,7 +182,7 @@ pub fn acquire(py: Python, array: *mut PyArrayObject) -> Result<(), BorrowError>
182182
}
183183
}
184184

185-
pub fn acquire_mut(py: Python, array: *mut PyArrayObject) -> Result<(), BorrowError> {
185+
pub fn acquire_mut<'py>(py: Python<'py>, array: *mut PyArrayObject) -> Result<(), BorrowError> {
186186
let shared = get_or_insert_shared(py).expect("Interal borrow checking API error");
187187

188188
let rc = unsafe { (shared.acquire_mut)(shared.flags, array) };
@@ -195,15 +195,15 @@ pub fn acquire_mut(py: Python, array: *mut PyArrayObject) -> Result<(), BorrowEr
195195
}
196196
}
197197

198-
pub fn release(py: Python, array: *mut PyArrayObject) {
198+
pub fn release<'py>(py: Python<'py>, array: *mut PyArrayObject) {
199199
let shared = get_or_insert_shared(py).expect("Interal borrow checking API error");
200200

201201
unsafe {
202202
(shared.release)(shared.flags, array);
203203
}
204204
}
205205

206-
pub fn release_mut(py: Python, array: *mut PyArrayObject) {
206+
pub fn release_mut<'py>(py: Python<'py>, array: *mut PyArrayObject) {
207207
let shared = get_or_insert_shared(py).expect("Interal borrow checking API error");
208208

209209
unsafe {
@@ -365,7 +365,7 @@ impl BorrowFlags {
365365
}
366366
}
367367

368-
fn base_address(py: Python, mut array: *mut PyArrayObject) -> *mut c_void {
368+
fn base_address<'py>(py: Python<'py>, mut array: *mut PyArrayObject) -> *mut c_void {
369369
loop {
370370
let base = unsafe { (*array).base };
371371

@@ -450,7 +450,7 @@ mod tests {
450450
use crate::array::{PyArray, PyArray1, PyArray2, PyArray3};
451451
use crate::convert::IntoPyArray;
452452

453-
fn get_borrow_flags<'py>(py: Python) -> &'py BorrowFlagsInner {
453+
fn get_borrow_flags<'py>(py: Python<'py>) -> &'py BorrowFlagsInner {
454454
let shared = get_or_insert_shared(py).unwrap();
455455
assert_eq!(shared.version, 1);
456456
unsafe { &(*(shared.flags as *mut BorrowFlags)).0 }

0 commit comments

Comments
 (0)