Introduce Task::map_with
This commit is contained in:
parent
06ece6a8c3
commit
9f21eae152
5 changed files with 55 additions and 11 deletions
|
|
@ -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.
|
||||
///
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue