Skip to content

Commit

Permalink
Merge pull request #469 from Icxolu/sync
Browse files Browse the repository at this point in the history
make `PySliceContainer` implement `Sync`
  • Loading branch information
Icxolu authored Nov 20, 2024
2 parents 0f1dd03 + 82d0466 commit 71a82a3
Show file tree
Hide file tree
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
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

0 comments on commit 71a82a3

Please sign in to comment.