From cb2a88b162638e8627e09e4ea11e278b4b9d175c Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Wed, 7 Feb 2024 10:20:42 -0700 Subject: [PATCH 1/2] Implement Send+Sync for VarLenAscii/Unicode/Array --- hdf5-types/src/array.rs | 7 +++++++ hdf5-types/src/string.rs | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/hdf5-types/src/array.rs b/hdf5-types/src/array.rs index f5b8956c5..f21feff4f 100644 --- a/hdf5-types/src/array.rs +++ b/hdf5-types/src/array.rs @@ -142,6 +142,13 @@ impl fmt::Debug for VarLenArray { } } +// Safety: `VarLenArray` allocates and frees memory for its data, which is copied +// from its original location; it never holds a reference to the data used to create it. +// Therefore if `T` is Send, so is `VarLenArray` +unsafe impl Send for VarLenArray {} +// Safety: No interior mutability or potential for data races if `T` is `Sync`. +unsafe impl Sync for VarLenArray {} + #[cfg(test)] pub mod tests { use super::VarLenArray; diff --git a/hdf5-types/src/string.rs b/hdf5-types/src/string.rs index 5f587f500..17e65d464 100644 --- a/hdf5-types/src/string.rs +++ b/hdf5-types/src/string.rs @@ -282,6 +282,12 @@ impl AsAsciiStr for VarLenAscii { } } +// Safety: `VarLenAscii` allocates and frees memory for its data, which is copied +// from its original location; it never holds a reference to the data used to create it. +unsafe impl Send for VarLenAscii {} +// Safety: No interior mutability or potential for data races. +unsafe impl Sync for VarLenAscii {} + // ================================================================================ #[repr(C)] @@ -371,6 +377,12 @@ impl FromStr for VarLenUnicode { } } +// Safety: `VarLenUnicode` allocates and frees memory for its data, which is copied +// from its original location; it never holds a reference to the data used to create it. +unsafe impl Send for VarLenUnicode {} +// Safety: No interior mutability or potential for data races. +unsafe impl Sync for VarLenUnicode {} + // ================================================================================ #[repr(C)] From 1455a777ea39f2a880b024788f596b5d596ae43a Mon Sep 17 00:00:00 2001 From: Craig Watson <686868+watsaig@users.noreply.github.com> Date: Tue, 13 Feb 2024 14:34:29 -0700 Subject: [PATCH 2/2] Apply suggestions from review Co-authored-by: Magnus Ulimoen Signed-off-by: Craig Watson <686868+watsaig@users.noreply.github.com> --- hdf5-types/src/array.rs | 6 ++---- hdf5-types/src/string.rs | 10 ++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/hdf5-types/src/array.rs b/hdf5-types/src/array.rs index f21feff4f..cf0909246 100644 --- a/hdf5-types/src/array.rs +++ b/hdf5-types/src/array.rs @@ -142,11 +142,9 @@ impl fmt::Debug for VarLenArray { } } -// Safety: `VarLenArray` allocates and frees memory for its data, which is copied -// from its original location; it never holds a reference to the data used to create it. -// Therefore if `T` is Send, so is `VarLenArray` +// Safety: Memory backed by `VarLenArray` can be accessed and freed from any thread unsafe impl Send for VarLenArray {} -// Safety: No interior mutability or potential for data races if `T` is `Sync`. +// Safety: `VarLenArray` has no interior mutability unsafe impl Sync for VarLenArray {} #[cfg(test)] diff --git a/hdf5-types/src/string.rs b/hdf5-types/src/string.rs index 17e65d464..612f462c7 100644 --- a/hdf5-types/src/string.rs +++ b/hdf5-types/src/string.rs @@ -282,10 +282,9 @@ impl AsAsciiStr for VarLenAscii { } } -// Safety: `VarLenAscii` allocates and frees memory for its data, which is copied -// from its original location; it never holds a reference to the data used to create it. +// Safety: Memory backed by `VarLenAscii` can be accessed and freed from any thread unsafe impl Send for VarLenAscii {} -// Safety: No interior mutability or potential for data races. +// Safety: `VarLenAscii` has no interior mutability unsafe impl Sync for VarLenAscii {} // ================================================================================ @@ -377,10 +376,9 @@ impl FromStr for VarLenUnicode { } } -// Safety: `VarLenUnicode` allocates and frees memory for its data, which is copied -// from its original location; it never holds a reference to the data used to create it. +// Safety: Memory backed by `VarLenUnicode` can be accessed and freed from any thread unsafe impl Send for VarLenUnicode {} -// Safety: No interior mutability or potential for data races. +// Safety: `VarLenUnicode` has no interior mutability unsafe impl Sync for VarLenUnicode {} // ================================================================================