From 82d04668df4edd4c1236c87796265cbf9c191bd4 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Tue, 19 Nov 2024 22:20:14 +0100 Subject: [PATCH] make `PySliceContainer` `Sync` --- src/dtype.rs | 2 +- src/slice_container.rs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/dtype.rs b/src/dtype.rs index b6cbea0b4..e22ac20ac 100644 --- a/src/dtype.rs +++ b/src/dtype.rs @@ -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 diff --git a/src/slice_container.rs b/src/slice_container.rs index b1326746b..0c29eae61 100644 --- a/src/slice_container.rs +++ b/src/slice_container.rs @@ -13,9 +13,17 @@ pub(crate) struct PySliceContainer { drop: unsafe fn(*mut u8, usize, usize), } +// This resembles `unsafe impl Send for PySliceContainer {}` 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 From> for PySliceContainer { +// This resembles `unsafe impl Sync for PySliceContainer {}` 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 From> for PySliceContainer { fn from(data: Box<[T]>) -> Self { unsafe fn drop_boxed_slice(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 From> for PySliceContainer { } } -impl From> for PySliceContainer { +impl From> for PySliceContainer { fn from(data: Vec) -> Self { unsafe fn drop_vec(ptr: *mut u8, len: usize, cap: usize) { let _ = Vec::from_raw_parts(ptr as *mut T, len, cap); @@ -65,7 +73,7 @@ impl From> for PySliceContainer { impl From, D>> for PySliceContainer where - A: Send, + A: Send + Sync, D: Dimension, { fn from(data: ArrayBase, D>) -> Self {