diff options
author | 2024-01-19 20:41:52 +0100 | |
---|---|---|
committer | 2024-01-19 20:41:52 +0100 | |
commit | 1781068e1c3a65551db1e832fdbaddba99124051 (patch) | |
tree | 60e0b3854cc0541712572fbb0e56f14435951ea9 /futures | |
parent | 41dec5bd203ff5b1574a33a17d5f7358ae1beea2 (diff) | |
parent | 7ae7fcb89855002519bab752fd3686106ce448db (diff) | |
download | iced-1781068e1c3a65551db1e832fdbaddba99124051.tar.gz iced-1781068e1c3a65551db1e832fdbaddba99124051.tar.bz2 iced-1781068e1c3a65551db1e832fdbaddba99124051.zip |
Merge branch 'master' into remove-vertex-indexing
Diffstat (limited to 'futures')
-rw-r--r-- | futures/src/event.rs | 2 | ||||
-rw-r--r-- | futures/src/keyboard.rs | 16 | ||||
-rw-r--r-- | futures/src/lib.rs | 4 | ||||
-rw-r--r-- | futures/src/maybe.rs | 35 | ||||
-rw-r--r-- | futures/src/maybe_send.rs | 21 | ||||
-rw-r--r-- | futures/src/runtime.rs | 25 |
6 files changed, 70 insertions, 33 deletions
diff --git a/futures/src/event.rs b/futures/src/event.rs index 214d2d40..97224506 100644 --- a/futures/src/event.rs +++ b/futures/src/event.rs @@ -35,7 +35,7 @@ where subscription::filter_map( (EventsWith, f), move |event, status| match event { - Event::Window(window::Event::RedrawRequested(_)) => None, + Event::Window(_, window::Event::RedrawRequested(_)) => None, _ => f(event, status), }, ) diff --git a/futures/src/keyboard.rs b/futures/src/keyboard.rs index af68e1f2..8e7da38f 100644 --- a/futures/src/keyboard.rs +++ b/futures/src/keyboard.rs @@ -1,6 +1,6 @@ //! Listen to keyboard events. use crate::core; -use crate::core::keyboard::{Event, KeyCode, Modifiers}; +use crate::core::keyboard::{Event, Key, Modifiers}; use crate::subscription::{self, Subscription}; use crate::MaybeSend; @@ -10,7 +10,7 @@ use crate::MaybeSend; /// If the function returns `None`, the key press will be simply /// ignored. pub fn on_key_press<Message>( - f: fn(KeyCode, Modifiers) -> Option<Message>, + f: fn(Key, Modifiers) -> Option<Message>, ) -> Subscription<Message> where Message: MaybeSend + 'static, @@ -22,11 +22,10 @@ where match (event, status) { ( core::Event::Keyboard(Event::KeyPressed { - key_code, - modifiers, + key, modifiers, .. }), core::event::Status::Ignored, - ) => f(key_code, modifiers), + ) => f(key, modifiers), _ => None, } }) @@ -38,7 +37,7 @@ where /// If the function returns `None`, the key release will be simply /// ignored. pub fn on_key_release<Message>( - f: fn(KeyCode, Modifiers) -> Option<Message>, + f: fn(Key, Modifiers) -> Option<Message>, ) -> Subscription<Message> where Message: MaybeSend + 'static, @@ -50,11 +49,12 @@ where match (event, status) { ( core::Event::Keyboard(Event::KeyReleased { - key_code, + key, modifiers, + .. }), core::event::Status::Ignored, - ) => f(key_code, modifiers), + ) => f(key, modifiers), _ => None, } }) diff --git a/futures/src/lib.rs b/futures/src/lib.rs index d54ba18a..b0acb76f 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -15,7 +15,7 @@ pub use futures; pub use iced_core as core; -mod maybe_send; +mod maybe; mod runtime; pub mod backend; @@ -25,7 +25,7 @@ pub mod keyboard; pub mod subscription; pub use executor::Executor; -pub use maybe_send::MaybeSend; +pub use maybe::{MaybeSend, MaybeSync}; pub use platform::*; pub use runtime::Runtime; pub use subscription::Subscription; diff --git a/futures/src/maybe.rs b/futures/src/maybe.rs new file mode 100644 index 00000000..c6a507c1 --- /dev/null +++ b/futures/src/maybe.rs @@ -0,0 +1,35 @@ +#[cfg(not(target_arch = "wasm32"))] +mod platform { + /// An extension trait that enforces `Send` only on native platforms. + /// + /// Useful for writing cross-platform async code! + pub trait MaybeSend: Send {} + + impl<T> MaybeSend for T where T: Send {} + + /// An extension trait that enforces `Sync` only on native platforms. + /// + /// Useful for writing cross-platform async code! + pub trait MaybeSync: Sync {} + + impl<T> MaybeSync for T where T: Sync {} +} + +#[cfg(target_arch = "wasm32")] +mod platform { + /// An extension trait that enforces `Send` only on native platforms. + /// + /// Useful for writing cross-platform async code! + pub trait MaybeSend {} + + impl<T> MaybeSend for T {} + + /// An extension trait that enforces `Sync` only on native platforms. + /// + /// Useful for writing cross-platform async code! + pub trait MaybeSync {} + + impl<T> MaybeSync for T {} +} + +pub use platform::{MaybeSend, MaybeSync}; diff --git a/futures/src/maybe_send.rs b/futures/src/maybe_send.rs deleted file mode 100644 index a6670f0e..00000000 --- a/futures/src/maybe_send.rs +++ /dev/null @@ -1,21 +0,0 @@ -#[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<T> 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<T> MaybeSend for T {} -} - -pub use platform::MaybeSend; diff --git a/futures/src/runtime.rs b/futures/src/runtime.rs index 16111b36..cac7b7e1 100644 --- a/futures/src/runtime.rs +++ b/futures/src/runtime.rs @@ -1,7 +1,7 @@ //! Run commands and keep track of subscriptions. use crate::core::event::{self, Event}; use crate::subscription; -use crate::{BoxFuture, Executor, MaybeSend}; +use crate::{BoxFuture, BoxStream, Executor, MaybeSend}; use futures::{channel::mpsc, Sink}; use std::marker::PhantomData; @@ -69,6 +69,29 @@ where self.executor.spawn(future); } + /// Runs a [`Stream`] in the [`Runtime`] until completion. + /// + /// The resulting `Message`s will be forwarded to the `Sender` of the + /// [`Runtime`]. + /// + /// [`Stream`]: BoxStream + pub fn run(&mut self, stream: BoxStream<Message>) { + use futures::{FutureExt, StreamExt}; + + let sender = self.sender.clone(); + let future = + stream.map(Ok).forward(sender).map(|result| match result { + Ok(()) => (), + Err(error) => { + log::warn!( + "Stream could not run until completion: {error}" + ); + } + }); + + self.executor.spawn(future); + } + /// Tracks a [`Subscription`] in the [`Runtime`]. /// /// It will spawn new streams or close old ones as necessary! See |