Skip to content

Commit

Permalink
Introduce Task::map_with
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Feb 11, 2025
1 parent 06ece6a commit 9f21eae
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/download_progress/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
18 changes: 8 additions & 10 deletions examples/gallery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand Down
1 change: 1 addition & 0 deletions futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions runtime/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]:

Check failure on line 106 in runtime/src/task.rs

View workflow job for this annotation

GitHub Actions / all

unresolved link to `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:

Check failure on line 119 in runtime/src/task.rs

View workflow job for this annotation

GitHub Actions / all

unresolved link to `map_with`
///
/// ```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.
///
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9f21eae

Please sign in to comment.