cosmic/
task.rs

1// Copyright 2023 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! Create asynchronous actions to be performed in the background.
5
6use futures::stream::{Stream, StreamExt};
7use std::future::Future;
8
9/// Yields a task which contains a batch of tasks.
10pub fn batch<X: Send + 'static + Into<Y>, Y: Send + 'static>(
11    tasks: impl IntoIterator<Item = iced::Task<X>>,
12) -> iced::Task<Y> {
13    iced::Task::batch(tasks).map(Into::into)
14}
15
16/// Yields a task which will run the future on the runtime executor.
17pub fn future<X: Into<Y>, Y: 'static>(
18    future: impl Future<Output = X> + Send + 'static,
19) -> iced::Task<Y> {
20    iced::Task::future(async move { future.await.into() })
21}
22
23/// Yields a task which will return a message.
24pub fn message<X: Send + 'static + Into<Y>, Y: 'static>(message: X) -> iced::Task<Y> {
25    future(async move { message.into() })
26}
27
28/// Yields a task which will run a stream on the runtime executor.
29pub fn stream<X: Into<Y> + 'static, Y: 'static>(
30    stream: impl Stream<Item = X> + Send + 'static,
31) -> iced::Task<Y> {
32    iced::Task::stream(stream.map(Into::into))
33}