Skip to content

Commit 7176bce

Browse files
committed
Use a more specific name for the Owner type, i.e. PySliceContainer.
1 parent dd675ba commit 7176bce

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

src/array.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::{iter::ExactSizeIterator, marker::PhantomData};
1717
use crate::convert::{ArrayExt, IntoPyArray, NpyIndex, ToNpyDims, ToPyArray};
1818
use crate::dtype::{DataType, Element};
1919
use crate::error::{FromVecError, NotContiguousError, ShapeError};
20-
use crate::owner::Owner;
20+
use crate::slice_container::PySliceContainer;
2121

2222
/// A safe, static-typed interface for
2323
/// [NumPy ndarray](https://numpy.org/doc/stable/reference/arrays.ndarray.html).
@@ -447,7 +447,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
447447
dims: ID,
448448
strides: *const npy_intp,
449449
data_ptr: *const T,
450-
owner: *mut PyAny,
450+
container: *mut PyAny,
451451
) -> &'py Self
452452
where
453453
ID: IntoDimension<Dim = D>,
@@ -467,36 +467,36 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
467467

468468
PY_ARRAY_API.PyArray_SetBaseObject(
469469
ptr as *mut npyffi::PyArrayObject,
470-
owner as *mut ffi::PyObject,
470+
container as *mut ffi::PyObject,
471471
);
472472

473473
Self::from_owned_ptr(py, ptr)
474474
}
475475

476-
pub(crate) unsafe fn from_raw_parts<'py, ID, O>(
476+
pub(crate) unsafe fn from_raw_parts<'py, ID, C>(
477477
py: Python<'py>,
478478
dims: ID,
479479
strides: *const npy_intp,
480480
data_ptr: *const T,
481-
owner: O,
481+
container: C,
482482
) -> &'py Self
483483
where
484484
ID: IntoDimension<Dim = D>,
485-
Owner: From<O>,
485+
PySliceContainer: From<C>,
486486
{
487-
let owner = pyo3::PyClassInitializer::from(Owner::from(owner))
487+
let container = pyo3::PyClassInitializer::from(PySliceContainer::from(container))
488488
.create_cell(py)
489489
.expect("Object creation failed.");
490490

491-
Self::new_with_data(py, dims, strides, data_ptr, owner as *mut PyAny)
491+
Self::new_with_data(py, dims, strides, data_ptr, container as *mut PyAny)
492492
}
493493

494-
/// Creates a NumPy array backed by `array` and ties its ownership to the Python object `owner`.
494+
/// Creates a NumPy array backed by `array` and ties its ownership to the Python object `container`.
495495
///
496496
/// # Safety
497497
///
498-
/// `owner` is set as a base object of the returned array which must not be dropped until `owner` is dropped.
499-
/// Furthermore, `array` must not be reallocated from the time this method is called and until `owner` is dropped.
498+
/// `container` is set as a base object of the returned array which must not be dropped until `container` is dropped.
499+
/// Furthermore, `array` must not be reallocated from the time this method is called and until `container` is dropped.
500500
///
501501
/// # Example
502502
///
@@ -521,21 +521,26 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
521521
/// }
522522
/// }
523523
/// ```
524-
pub unsafe fn borrow_from_array<'py, S>(array: &ArrayBase<S, D>, owner: &'py PyAny) -> &'py Self
524+
pub unsafe fn borrow_from_array<'py, S>(
525+
array: &ArrayBase<S, D>,
526+
container: &'py PyAny,
527+
) -> &'py Self
525528
where
526529
S: Data<Elem = T>,
527530
{
528531
let (strides, dims) = (array.npy_strides(), array.raw_dim());
529532
let data_ptr = array.as_ptr();
530533

531-
mem::forget(owner.to_object(owner.py()));
534+
let py = container.py();
535+
536+
mem::forget(container.to_object(py));
532537

533538
Self::new_with_data(
534-
owner.py(),
539+
py,
535540
dims,
536541
strides.as_ptr(),
537542
data_ptr,
538-
owner as *const PyAny as *mut PyAny,
543+
container as *const PyAny as *mut PyAny,
539544
)
540545
}
541546

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ mod dtype;
4141
mod error;
4242
pub mod npyffi;
4343
pub mod npyiter;
44-
mod owner;
4544
mod readonly;
45+
mod slice_container;
4646
mod sum_products;
4747

4848
pub use ndarray;

src/owner.rs renamed to src/slice_container.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ use pyo3::{ffi, types::PyAny, PyCell};
99

1010
use crate::dtype::Element;
1111

12-
pub(crate) struct Owner {
12+
/// Utility type to safely store Box<[_]> or Vec<_> on the Python heap
13+
pub(crate) struct PySliceContainer {
1314
ptr: *mut u8,
1415
len: usize,
1516
cap: usize,
1617
drop: unsafe fn(*mut u8, usize, usize),
1718
}
1819

19-
unsafe impl Send for Owner {}
20+
unsafe impl Send for PySliceContainer {}
2021

21-
impl<T: Send> From<Box<[T]>> for Owner {
22+
impl<T: Send> From<Box<[T]>> for PySliceContainer {
2223
fn from(data: Box<[T]>) -> Self {
2324
unsafe fn drop_boxed_slice<T>(ptr: *mut u8, len: usize, _cap: usize) {
2425
let _ = Box::from_raw(slice::from_raw_parts_mut(ptr as *mut T, len) as *mut [T]);
2526
}
2627

28+
// FIXME(adamreichold): Use `Box::into_raw` when
29+
// `*mut [T]::{as_mut_ptr, len}` become stable and compatible with out MSRV.
2730
let ptr = data.as_ptr() as *mut u8;
2831
let len = data.len();
2932
let cap = 0;
@@ -40,12 +43,14 @@ impl<T: Send> From<Box<[T]>> for Owner {
4043
}
4144
}
4245

43-
impl<T: Send> From<Vec<T>> for Owner {
46+
impl<T: Send> From<Vec<T>> for PySliceContainer {
4447
fn from(data: Vec<T>) -> Self {
4548
unsafe fn drop_vec<T>(ptr: *mut u8, len: usize, cap: usize) {
4649
let _ = Vec::from_raw_parts(ptr as *mut T, len, cap);
4750
}
4851

52+
// FIXME(adamreichold): Use `Vec::into_raw_parts`
53+
// when it becomes stable and compatible with our MSRV.
4954
let ptr = data.as_ptr() as *mut u8;
5055
let len = data.len();
5156
let cap = data.capacity();
@@ -62,7 +67,7 @@ impl<T: Send> From<Vec<T>> for Owner {
6267
}
6368
}
6469

65-
impl<A, D> From<ArrayBase<OwnedRepr<A>, D>> for Owner
70+
impl<A, D> From<ArrayBase<OwnedRepr<A>, D>> for PySliceContainer
6671
where
6772
A: Element,
6873
D: Dimension,
@@ -72,32 +77,32 @@ where
7277
}
7378
}
7479

75-
impl Drop for Owner {
80+
impl Drop for PySliceContainer {
7681
fn drop(&mut self) {
7782
unsafe {
7883
(self.drop)(self.ptr, self.len, self.cap);
7984
}
8085
}
8186
}
8287

83-
impl PyClass for Owner {
88+
impl PyClass for PySliceContainer {
8489
type Dict = PyClassDummySlot;
8590
type WeakRef = PyClassDummySlot;
8691
type BaseNativeType = PyAny;
8792
}
8893

89-
impl PyClassImpl for Owner {
90-
const DOC: &'static str = "Memory store for a PyArray backed by a Rust type \0";
94+
impl PyClassImpl for PySliceContainer {
95+
const DOC: &'static str = "Memory store for a PyArray backed by a Box<[_]> or a Vec<_> \0";
9196

9297
type BaseType = PyAny;
9398
type Layout = PyCell<Self>;
9499
type ThreadChecker = ThreadCheckerStub<Self>;
95100
}
96101

97-
unsafe impl PyTypeInfo for Owner {
102+
unsafe impl PyTypeInfo for PySliceContainer {
98103
type AsRefTarget = PyCell<Self>;
99104

100-
const NAME: &'static str = "Owner";
105+
const NAME: &'static str = "PySliceContainer";
101106
const MODULE: Option<&'static str> = Some("_rust_numpy");
102107

103108
#[inline]

0 commit comments

Comments
 (0)