diff options
| author | 2025-02-09 11:10:54 +0100 | |
|---|---|---|
| committer | 2025-02-09 11:14:02 +0100 | |
| commit | c8981d00963dac0dc03e6351255ba8823519162c (patch) | |
| tree | c3a8fcc5655404875e6a320e95a8a8822ed3b5c9 /runtime | |
| parent | 12653114bd1cadb10b9fe185b6f2b1e4300bdcf7 (diff) | |
| download | iced-c8981d00963dac0dc03e6351255ba8823519162c.tar.gz iced-c8981d00963dac0dc03e6351255ba8823519162c.tar.bz2 iced-c8981d00963dac0dc03e6351255ba8823519162c.zip | |
Implement `sipper` support through `Task::sip` :tada:
Diffstat (limited to '')
| -rw-r--r-- | runtime/Cargo.toml | 3 | ||||
| -rw-r--r-- | runtime/src/task.rs | 23 | 
2 files changed, 24 insertions, 2 deletions
| diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 703c3ed9..fc212ef8 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -23,5 +23,6 @@ iced_core.workspace = true  iced_futures.workspace = true  iced_futures.features = ["thread-pool"] -thiserror.workspace = true  raw-window-handle.workspace = true +sipper.workspace = true +thiserror.workspace = true diff --git a/runtime/src/task.rs b/runtime/src/task.rs index 22cfb63e..fb46ca45 100644 --- a/runtime/src/task.rs +++ b/runtime/src/task.rs @@ -3,7 +3,6 @@ use crate::core::widget;  use crate::futures::futures::channel::mpsc;  use crate::futures::futures::channel::oneshot;  use crate::futures::futures::future::{self, FutureExt}; -use crate::futures::futures::never::Never;  use crate::futures::futures::stream::{self, Stream, StreamExt};  use crate::futures::{boxed_stream, BoxStream, MaybeSend};  use crate::Action; @@ -11,6 +10,9 @@ use crate::Action;  use std::future::Future;  use std::sync::Arc; +#[doc(no_inline)] +pub use sipper::{sipper, stream, Never, Sender, Sipper, Straw}; +  /// A set of concurrent actions to be performed by the iced runtime.  ///  /// A [`Task`] _may_ produce a bunch of values of type `T`. @@ -57,6 +59,25 @@ impl<T> Task<T> {          Self::stream(stream.map(f))      } +    /// Creates a [`Task`] that runs the given [`Sipper`] to completion, mapping +    /// progress with the first closure and the output with the second one. +    pub fn sip<S, Output, Progress>( +        sipper: S, +        on_progress: impl Fn(Progress) -> T + Send + 'static, +        on_output: impl FnOnce(Output) -> T + Send + 'static, +    ) -> Self +    where +        S: Sipper<Output, Progress> + Send + 'static, +        S::Future: Send + 'static, +        Output: Send, +        Progress: Send, +        T: Send + 'static, +    { +        Self::stream(stream(sipper::sipper(move |sender| async move { +            on_output(sipper.map(on_progress).run(sender).await) +        }))) +    } +      /// Combines the given tasks and produces a single [`Task`] that will run all of them      /// in parallel.      pub fn batch(tasks: impl IntoIterator<Item = Self>) -> Self | 
