Skip to content

Commit ffb15c2

Browse files
committed
Implement sipper support through Task::sip 🎉
1 parent 1265311 commit ffb15c2

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ raw-window-handle = "0.6"
172172
resvg = "0.42"
173173
rustc-hash = "2.0"
174174
sha2 = "0.10"
175+
sipper = "0.0.4"
175176
smol = "1.0"
176177
smol_str = "0.2"
177178
softbuffer = "0.4"

runtime/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ iced_core.workspace = true
2323
iced_futures.workspace = true
2424
iced_futures.features = ["thread-pool"]
2525

26-
thiserror.workspace = true
2726
raw-window-handle.workspace = true
27+
sipper.workspace = true
28+
thiserror.workspace = true

runtime/src/task.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ use crate::core::widget;
33
use crate::futures::futures::channel::mpsc;
44
use crate::futures::futures::channel::oneshot;
55
use crate::futures::futures::future::{self, FutureExt};
6-
use crate::futures::futures::never::Never;
76
use crate::futures::futures::stream::{self, Stream, StreamExt};
87
use crate::futures::{boxed_stream, BoxStream, MaybeSend};
98
use crate::Action;
109

1110
use std::future::Future;
1211
use std::sync::Arc;
1312

13+
#[doc(no_inline)]
14+
pub use sipper::{sipper, stream, Never, Sender, Sipper, Straw};
15+
1416
/// A set of concurrent actions to be performed by the iced runtime.
1517
///
1618
/// A [`Task`] _may_ produce a bunch of values of type `T`.
@@ -57,6 +59,25 @@ impl<T> Task<T> {
5759
Self::stream(stream.map(f))
5860
}
5961

62+
/// Creates a [`Task`] that runs the given [`Sipper`] to completion, mapping
63+
/// progress with the first closure and the output with the second one.
64+
pub fn sip<S, Output, Progress>(
65+
sipper: S,
66+
on_progress: impl Fn(Progress) -> T + MaybeSend + 'static,
67+
on_output: impl FnOnce(Output) -> T + MaybeSend + 'static,
68+
) -> Self
69+
where
70+
S: Sipper<Output, Progress> + MaybeSend + 'static,
71+
S::Future: MaybeSend + 'static,
72+
Output: MaybeSend,
73+
Progress: MaybeSend,
74+
T: MaybeSend + 'static,
75+
{
76+
Self::stream(stream(sipper::sipper(move |sender| async move {
77+
on_output(sipper.map(on_progress).run(sender).await)
78+
})))
79+
}
80+
6081
/// Combines the given tasks and produces a single [`Task`] that will run all of them
6182
/// in parallel.
6283
pub fn batch(tasks: impl IntoIterator<Item = Self>) -> Self

0 commit comments

Comments
 (0)