Skip to content
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

make PySliceContainer implement Sync #469

Merged
merged 1 commit into from
Nov 20, 2024
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/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/slice_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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 {
Expand Down