Skip to content

Commit

Permalink
make PySliceContainer Sync
Browse files Browse the repository at this point in the history
Icxolu committed Nov 19, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent 0f1dd03 commit 82d0466
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/dtype.rs
Original file line number Diff line number Diff line change
@@ -697,7 +697,7 @@ impl Sealed for Bound<'_, PyArrayDescr> {}
///
/// [enumerated-types]: https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types
/// [data-models]: https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models
pub unsafe trait Element: Sized + Send {
pub unsafe trait Element: Sized + Send + Sync {
/// Flag that indicates whether this type is trivially copyable.
///
/// It should be set to true for all trivially copyable types (like scalar types
14 changes: 11 additions & 3 deletions src/slice_container.rs
Original file line number Diff line number Diff line change
@@ -13,9 +13,17 @@ pub(crate) struct PySliceContainer {
drop: unsafe fn(*mut u8, usize, usize),
}

// This resembles `unsafe impl<T: Send> Send for PySliceContainer<T> {}` if we
// were allow to use a generic there.
// SAFETY: Every construction below enforces `T: Send` fulfilling the ideal bound above
unsafe impl Send for PySliceContainer {}

impl<T: Send> From<Box<[T]>> for PySliceContainer {
// This resembles `unsafe impl<T: Sync> Sync for PySliceContainer<T> {}` if we
// were allow to use a generic there.
// SAFETY: Every construction below enforces `T: Sync` fulfilling the ideal bound above
unsafe impl Sync for PySliceContainer {}

impl<T: Send + Sync> From<Box<[T]>> for PySliceContainer {
fn from(data: Box<[T]>) -> Self {
unsafe fn drop_boxed_slice<T>(ptr: *mut u8, len: usize, _cap: usize) {
let _ = Box::from_raw(ptr::slice_from_raw_parts_mut(ptr as *mut T, len));
@@ -39,7 +47,7 @@ impl<T: Send> From<Box<[T]>> for PySliceContainer {
}
}

impl<T: Send> From<Vec<T>> for PySliceContainer {
impl<T: Send + Sync> From<Vec<T>> for PySliceContainer {
fn from(data: Vec<T>) -> Self {
unsafe fn drop_vec<T>(ptr: *mut u8, len: usize, cap: usize) {
let _ = Vec::from_raw_parts(ptr as *mut T, len, cap);
@@ -65,7 +73,7 @@ impl<T: Send> From<Vec<T>> for PySliceContainer {

impl<A, D> From<ArrayBase<OwnedRepr<A>, D>> for PySliceContainer
where
A: Send,
A: Send + Sync,
D: Dimension,
{
fn from(data: ArrayBase<OwnedRepr<A>, D>) -> Self {

0 comments on commit 82d0466

Please sign in to comment.