From ae7c17263e3be313e2b6c8a39e034809657dad8c Mon Sep 17 00:00:00 2001 From: Zac Harrold Date: Mon, 19 Feb 2024 15:08:48 +1100 Subject: [PATCH] Implement `Send` for `Stream` on certain platforms --- src/platform/maybe_send.rs | 23 +++++++++++++++++++++++ src/platform/mod.rs | 20 ++++---------------- 2 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 src/platform/maybe_send.rs diff --git a/src/platform/maybe_send.rs b/src/platform/maybe_send.rs new file mode 100644 index 000000000..f1cb0e8b2 --- /dev/null +++ b/src/platform/maybe_send.rs @@ -0,0 +1,23 @@ +//! The following zero-sized type is for applying [`Send`]/[`Sync`]` restrictions to ensure +//! consistent behaviour across different platforms. The verbosely named type is used +//! (rather than using the markers directly) in the hope of making the compile errors +//! slightly more helpful. + +// TODO: Remove this in favour of using negative trait bounds if they stabilise. + +/// A marker used to remove the `Send` and `Sync` traits. +pub(crate) struct NotSendSyncAcrossAllPlatforms(std::marker::PhantomData<*mut ()>); + +impl Default for NotSendSyncAcrossAllPlatforms { + fn default() -> Self { + NotSendSyncAcrossAllPlatforms(std::marker::PhantomData) + } +} + +// TODO: Implement Send on platforms which support it. + +#[cfg(any( + // Windows with WASAPI allows for a Send Stream + all(windows, not(feature = "asio")), +))] +unsafe impl Send for NotSendSyncAcrossAllPlatforms {} diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 4cb4d5a29..95b346dd4 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -4,6 +4,10 @@ //! type and its associated [`Device`], [`Stream`] and other associated types. These //! types are useful in the case that users require switching between audio host APIs at runtime. +mod maybe_send; + +pub(crate) use maybe_send::*; + #[doc(inline)] pub use self::platform_impl::*; @@ -724,19 +728,3 @@ mod platform_impl { .into() } } - -// The following zero-sized types are for applying Send/Sync restrictions to ensure -// consistent behaviour across different platforms. These verbosely named types are used -// (rather than using the markers directly) in the hope of making the compile errors -// slightly more helpful. -// -// TODO: Remove these in favour of using negative trait bounds if they stabilise. - -// A marker used to remove the `Send` and `Sync` traits. -struct NotSendSyncAcrossAllPlatforms(std::marker::PhantomData<*mut ()>); - -impl Default for NotSendSyncAcrossAllPlatforms { - fn default() -> Self { - NotSendSyncAcrossAllPlatforms(std::marker::PhantomData) - } -}