diff options
| author | 2025-02-11 02:34:10 +0100 | |
|---|---|---|
| committer | 2025-02-11 02:34:10 +0100 | |
| commit | 9f21eae1528fa414adbfb987ce4c851fa58326fe (patch) | |
| tree | 4eff76cd5effe1b3fa51e4fe17c9a53aa9823841 | |
| parent | 06ece6a8c30541b7b2b6bcbb370fdffc9f1fcb79 (diff) | |
| download | iced-9f21eae1528fa414adbfb987ce4c851fa58326fe.tar.gz iced-9f21eae1528fa414adbfb987ce4c851fa58326fe.tar.bz2 iced-9f21eae1528fa414adbfb987ce4c851fa58326fe.zip  | |
Introduce `Task::map_with`
| -rw-r--r-- | examples/download_progress/src/main.rs | 2 | ||||
| -rw-r--r-- | examples/gallery/src/main.rs | 18 | ||||
| -rw-r--r-- | futures/src/lib.rs | 1 | ||||
| -rw-r--r-- | runtime/src/task.rs | 44 | ||||
| -rw-r--r-- | src/lib.rs | 1 | 
5 files changed, 55 insertions, 11 deletions
diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index 8f59f1dc..cc7a7800 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -52,7 +52,7 @@ impl Example {                  let task = download.start(); -                task.map(move |update| Message::DownloadUpdated(index, update)) +                task.map_with(index, Message::DownloadUpdated)              }              Message::DownloadUpdated(id, update) => {                  if let Some(download) = diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs index ab22679d..441ad924 100644 --- a/examples/gallery/src/main.rs +++ b/examples/gallery/src/main.rs @@ -94,17 +94,15 @@ impl Gallery {                      return Task::none();                  }; -                Task::batch(vec![ -                    Task::perform( +                Task::batch([ +                    Task::future(                          image.clone().blurhash(Preview::WIDTH, Preview::HEIGHT), -                        move |result| Message::BlurhashDecoded(id, result), -                    ), -                    Task::perform( -                        image.download(Size::Thumbnail { -                            width: Preview::WIDTH, -                        }), -                        move |result| Message::ThumbnailDownloaded(id, result), -                    ), +                    ) +                    .map_with(id, Message::BlurhashDecoded), +                    Task::future(image.download(Size::Thumbnail { +                        width: Preview::WIDTH, +                    })) +                    .map_with(id, Message::ThumbnailDownloaded),                  ])              }              Message::ImageDownloaded(Ok(rgba)) => { diff --git a/futures/src/lib.rs b/futures/src/lib.rs index a874a618..31738823 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -15,6 +15,7 @@ pub mod backend;  pub mod event;  pub mod executor;  pub mod keyboard; +pub mod stream;  pub mod subscription;  pub use executor::Executor; diff --git a/runtime/src/task.rs b/runtime/src/task.rs index bfc36d75..1a1ef699 100644 --- a/runtime/src/task.rs +++ b/runtime/src/task.rs @@ -98,6 +98,50 @@ impl<T> Task<T> {          self.then(move |output| Task::done(f(output)))      } +    /// Combines a prefix value with the result of the [`Task`] using +    /// the provided closure. +    /// +    /// Sometimes you will want to identify the source or target +    /// of some [`Task`] in your UI. This can be achieved through +    /// normal means by using [`map`]: +    /// +    /// ```rust +    /// # use iced_runtime::Task; +    /// # let task = Task::none(); +    /// # enum Message { TaskCompleted(u32, ()) } +    /// let id = 123; +    /// +    /// # let _ = { +    /// task.map(move |result| Message::TaskCompleted(id, result)) +    /// # }; +    /// ``` +    /// +    /// Quite a mouthful. [`map_with`] lets you write: +    /// +    /// ```rust +    /// # use iced_runtime::Task; +    /// # let task = Task::none(); +    /// # enum Message { TaskCompleted(u32, ()) } +    /// # let id = 123; +    /// # let _ = { +    /// task.map_with(id, Message::TaskCompleted) +    /// # }; +    /// ``` +    /// +    /// Much nicer! +    pub fn map_with<P, O>( +        self, +        prefix: P, +        mut f: impl FnMut(P, T) -> O + MaybeSend + 'static, +    ) -> Task<O> +    where +        T: MaybeSend + 'static, +        P: MaybeSend + Clone + 'static, +        O: MaybeSend + 'static, +    { +        self.map(move |result| f(prefix.clone(), result)) +    } +      /// Performs a new [`Task`] for every output of the current [`Task`] using the      /// given closure.      /// @@ -478,6 +478,7 @@ use iced_winit::core;  use iced_winit::runtime;  pub use iced_futures::futures; +pub use iced_futures::stream;  #[cfg(feature = "highlighter")]  pub use iced_highlighter as highlighter;  | 
