Skip to content

Avoid raw pointers in SliceBox and make the Send bound explicit #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
{
let dims = dims.into_dimension();
let container = SliceBox::new(slice);
let data_ptr = container.data;
let data_ptr = container.data.as_ptr();
let cell = pyo3::PyClassInitializer::from(container)
.create_cell(py)
.expect("Object creation failed.");
Expand Down
2 changes: 1 addition & 1 deletion src/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl DataType {
}

/// Represents that a type can be an element of `PyArray`.
pub trait Element: Clone {
pub trait Element: Clone + Send {
/// `DataType` corresponding to this type.
const DATA_TYPE: DataType;

Expand Down
31 changes: 15 additions & 16 deletions src/slice_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,39 @@ use pyo3::pyclass_slots::PyClassDummySlot;
use pyo3::{ffi, type_object, types::PyAny, PyCell};

pub(crate) struct SliceBox<T> {
pub(crate) data: *mut [T],
pub(crate) data: Box<[T]>,
}

impl<T> SliceBox<T> {
pub(crate) fn new(value: Box<[T]>) -> Self {
SliceBox {
data: Box::into_raw(value),
}
pub(crate) fn new(data: Box<[T]>) -> Self {
Self { data }
}
}

impl<T> Drop for SliceBox<T> {
fn drop(&mut self) {
let _boxed_slice = unsafe { Box::from_raw(self.data) };
}
}

impl<T> PyClass for SliceBox<T> {
impl<T> PyClass for SliceBox<T>
where
T: Send,
{
type Dict = PyClassDummySlot;
type WeakRef = PyClassDummySlot;
type BaseNativeType = PyAny;
}

impl<T> PyClassImpl for SliceBox<T> {
impl<T> PyClassImpl for SliceBox<T>
where
T: Send,
{
const DOC: &'static str = "Memory store for PyArray using rust's Box<[T]> \0";

type BaseType = PyAny;
type Layout = PyCell<Self>;
type ThreadChecker = ThreadCheckerStub<Self>;
}

unsafe impl<T> type_object::PyTypeInfo for SliceBox<T> {
unsafe impl<T> type_object::PyTypeInfo for SliceBox<T>
where
T: Send,
{
type AsRefTarget = PyCell<Self>;
const NAME: &'static str = "SliceBox";
const MODULE: Option<&'static str> = Some("_rust_numpy");
Expand All @@ -47,5 +48,3 @@ unsafe impl<T> type_object::PyTypeInfo for SliceBox<T> {
TYPE_OBJECT.get_or_init::<Self>(py)
}
}

unsafe impl<T> Send for SliceBox<T> {}