From 5dab5a327ef643ee38ac3e42ab35212fff445631 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 28 Jan 2022 17:35:47 +0700 Subject: Introduce `MaybeSend` trait in `iced_futures` It allows to clean up all the `trait_aliases` modules! --- futures/src/executor.rs | 8 ++--- futures/src/lib.rs | 2 ++ futures/src/maybe_send.rs | 21 ++++++++++++++ futures/src/runtime.rs | 56 +++++------------------------------ futures/src/subscription/tracker.rs | 54 +++++----------------------------- native/src/subscription.rs | 58 ++++++------------------------------- 6 files changed, 49 insertions(+), 150 deletions(-) create mode 100644 futures/src/maybe_send.rs diff --git a/futures/src/executor.rs b/futures/src/executor.rs index 23682f32..34e329a0 100644 --- a/futures/src/executor.rs +++ b/futures/src/executor.rs @@ -33,6 +33,7 @@ pub use self::smol::Smol; #[cfg(target_arch = "wasm32")] pub use wasm_bindgen::WasmBindgen; +use crate::MaybeSend; use futures::Future; /// A type that can run futures. @@ -43,12 +44,7 @@ pub trait Executor: Sized { Self: Sized; /// Spawns a future in the [`Executor`]. - #[cfg(not(target_arch = "wasm32"))] - fn spawn(&self, future: impl Future + Send + 'static); - - /// Spawns a local future in the [`Executor`]. - #[cfg(target_arch = "wasm32")] - fn spawn(&self, future: impl Future + 'static); + fn spawn(&self, future: impl Future + MaybeSend + 'static); /// Runs the given closure inside the [`Executor`]. /// diff --git a/futures/src/lib.rs b/futures/src/lib.rs index dbcb8aca..10ade9ed 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -14,6 +14,7 @@ pub use futures; mod command; +mod maybe_send; mod runtime; pub mod executor; @@ -35,6 +36,7 @@ pub mod time; pub use command::Command; pub use executor::Executor; +pub use maybe_send::MaybeSend; pub use platform::*; pub use runtime::Runtime; pub use subscription::Subscription; diff --git a/futures/src/maybe_send.rs b/futures/src/maybe_send.rs new file mode 100644 index 00000000..a6670f0e --- /dev/null +++ b/futures/src/maybe_send.rs @@ -0,0 +1,21 @@ +#[cfg(not(target_arch = "wasm32"))] +mod platform { + /// An extension trait that enforces `Send` only on native platforms. + /// + /// Useful to write cross-platform async code! + pub trait MaybeSend: Send {} + + impl MaybeSend for T where T: Send {} +} + +#[cfg(target_arch = "wasm32")] +mod platform { + /// An extension trait that enforces `Send` only on native platforms. + /// + /// Useful to write cross-platform async code! + pub trait MaybeSend {} + + impl MaybeSend for T {} +} + +pub use platform::MaybeSend; diff --git a/futures/src/runtime.rs b/futures/src/runtime.rs index 96104cd9..2034ed6c 100644 --- a/futures/src/runtime.rs +++ b/futures/src/runtime.rs @@ -1,54 +1,10 @@ //! Run commands and keep track of subscriptions. -use crate::BoxFuture; -use crate::{subscription, Executor, Subscription}; +use crate::subscription; +use crate::{BoxFuture, Executor, MaybeSend, Subscription}; use futures::{channel::mpsc, Sink}; use std::marker::PhantomData; -#[cfg(not(target_arch = "wasm32"))] -mod trait_aliases { - use super::*; - - pub trait RuntimeMessage: Send + 'static {} - - impl RuntimeMessage for T where T: Send + 'static {} - - pub trait RuntimeMessageSender: - Sink + Unpin + Send + Clone + 'static - { - } - - impl RuntimeMessageSender for T where - T: Sink - + Unpin - + Send - + Clone - + 'static - { - } -} - -#[cfg(target_arch = "wasm32")] -mod trait_aliases { - use super::*; - - pub trait RuntimeMessage: 'static {} - - impl RuntimeMessage for T where T: 'static {} - - pub trait RuntimeMessageSender: - Sink + Unpin + Clone + 'static - { - } - - impl RuntimeMessageSender for T where - T: Sink + Unpin + Clone + 'static - { - } -} - -pub use trait_aliases::{RuntimeMessage, RuntimeMessageSender}; - /// A batteries-included runtime of commands and subscriptions. /// /// If you have an [`Executor`], a [`Runtime`] can be leveraged to run any @@ -67,8 +23,12 @@ where Hasher: std::hash::Hasher + Default, Event: Send + Clone + 'static, Executor: self::Executor, - Sender: RuntimeMessageSender, - Message: RuntimeMessage, + Sender: Sink + + Unpin + + MaybeSend + + Clone + + 'static, + Message: MaybeSend + 'static, { /// Creates a new empty [`Runtime`]. /// diff --git a/futures/src/subscription/tracker.rs b/futures/src/subscription/tracker.rs index 01e0c105..421fb917 100644 --- a/futures/src/subscription/tracker.rs +++ b/futures/src/subscription/tracker.rs @@ -1,52 +1,8 @@ -use crate::{BoxFuture, Subscription}; +use crate::{BoxFuture, MaybeSend, Subscription}; use futures::{channel::mpsc, sink::Sink}; use std::{collections::HashMap, marker::PhantomData}; -#[cfg(not(target_arch = "wasm32"))] -mod trait_aliases { - use super::*; - - pub trait TrackerMessage: Send + 'static {} - - impl TrackerMessage for T where T: Send + 'static {} - - pub trait TrackerMessageReceiver: - Sink + Unpin + Send + Clone + 'static - { - } - - impl TrackerMessageReceiver for T where - T: Sink - + Unpin - + Send - + Clone - + 'static - { - } -} - -#[cfg(target_arch = "wasm32")] -mod trait_aliases { - use super::*; - - pub trait TrackerMessage: 'static {} - - impl TrackerMessage for T where T: 'static {} - - pub trait TrackerMessageReceiver: - Sink + Unpin + Clone + 'static - { - } - - impl TrackerMessageReceiver for T where - T: Sink + Unpin + Clone + 'static - { - } -} - -pub use trait_aliases::{TrackerMessage, TrackerMessageReceiver}; - /// A registry of subscription streams. /// /// If you have an application that continuously returns a [`Subscription`], @@ -101,8 +57,12 @@ where receiver: Receiver, ) -> Vec> where - Message: TrackerMessage, - Receiver: TrackerMessageReceiver, + Message: 'static + MaybeSend, + Receiver: 'static + + Sink + + Unpin + + MaybeSend + + Clone, { use futures::{future::FutureExt, stream::StreamExt}; diff --git a/native/src/subscription.rs b/native/src/subscription.rs index 4fb7e760..9775c84b 100644 --- a/native/src/subscription.rs +++ b/native/src/subscription.rs @@ -3,41 +3,10 @@ use crate::event::{self, Event}; use crate::Hasher; use iced_futures::futures::{self, Future, Stream}; -use iced_futures::BoxStream; +use iced_futures::{BoxStream, MaybeSend}; use std::hash::Hash; -#[cfg(not(target_arch = "wasm32"))] -mod trait_aliases { - use super::*; - - /// Wrapper type - pub trait RunnerStream: - Stream + Send + 'static - { - } - - impl RunnerStream for T where - T: Stream + Send + 'static - { - } -} - -#[cfg(target_arch = "wasm32")] -mod trait_aliases { - use super::*; - - /// Wrapper type - pub trait RunnerStream: Stream + 'static {} - - impl RunnerStream for T where - T: Stream + 'static - { - } -} - -pub use trait_aliases::RunnerStream; - /// A request to listen to external events. /// /// Besides performing async actions on demand with [`Command`], most @@ -87,7 +56,7 @@ pub fn events_with( f: fn(Event, event::Status) -> Option, ) -> Subscription where - Message: 'static + Send, + Message: 'static + MaybeSend, { Subscription::from_recipe(Runner { id: f, @@ -109,7 +78,7 @@ where pub fn run(id: I, stream: S) -> Subscription where I: Hash + 'static, - S: Stream + Send + 'static, + S: Stream + MaybeSend + 'static, Message: 'static, { Subscription::from_recipe(Runner { @@ -190,13 +159,13 @@ where pub fn unfold( id: I, initial: T, - mut f: impl FnMut(T) -> Fut + Send + Sync + 'static, + mut f: impl FnMut(T) -> Fut + MaybeSend + Sync + 'static, ) -> Subscription where I: Hash + 'static, - T: Send + 'static, - Fut: Future, T)> + Send + 'static, - Message: 'static + Send, + T: MaybeSend + 'static, + Fut: Future, T)> + MaybeSend + 'static, + Message: 'static + MaybeSend, { use futures::future::{self, FutureExt}; use futures::stream::StreamExt; @@ -222,7 +191,7 @@ impl Recipe where I: Hash + 'static, F: FnOnce(EventStream) -> S, - S: RunnerStream, + S: Stream + MaybeSend + 'static, { type Output = Message; @@ -232,15 +201,6 @@ where } fn stream(self: Box, input: EventStream) -> BoxStream { - use futures::stream::StreamExt; - - #[cfg(target_arch = "wasm32")] - { - (self.spawn)(input).boxed_local() - } - #[cfg(not(target_arch = "wasm32"))] - { - (self.spawn)(input).boxed() - } + iced_futures::boxed_stream((self.spawn)(input)) } } -- cgit