Skip to content

Commit

Permalink
Implement sipper support through Task::sip 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Feb 9, 2025
1 parent 1265311 commit ffb15c2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ raw-window-handle = "0.6"
resvg = "0.42"
rustc-hash = "2.0"
sha2 = "0.10"
sipper = "0.0.4"
smol = "1.0"
smol_str = "0.2"
softbuffer = "0.4"
Expand Down
3 changes: 2 additions & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 22 additions & 1 deletion runtime/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ 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;

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`.
Expand Down Expand Up @@ -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 + MaybeSend + 'static,
on_output: impl FnOnce(Output) -> T + MaybeSend + 'static,
) -> Self
where
S: Sipper<Output, Progress> + MaybeSend + 'static,
S::Future: MaybeSend + 'static,
Output: MaybeSend,
Progress: MaybeSend,
T: MaybeSend + 'static,
{
Self::stream(stream(sipper::sipper(move |sender| async move {

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

View workflow job for this annotation

GitHub Actions / wasm

`T` cannot be sent between threads safely

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

View workflow job for this annotation

GitHub Actions / wasm

`T` cannot be sent between threads safely
on_output(sipper.map(on_progress).run(sender).await)

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

View workflow job for this annotation

GitHub Actions / wasm

`Progress` cannot be sent between threads safely

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

View workflow job for this annotation

GitHub Actions / wasm

`Output` cannot be sent between threads safely

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

View workflow job for this annotation

GitHub Actions / wasm

`impl Fn(Progress) -> T + MaybeSend + 'static` cannot be sent between threads safely

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

View workflow job for this annotation

GitHub Actions / wasm

`T` cannot be sent between threads safely

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

View workflow job for this annotation

GitHub Actions / wasm

the method `run` exists for associated type `impl Sipper<Output, T, Future = Pin<Box<...>>>`, but its trait bounds were not satisfied
})))
}

/// 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
Expand Down

0 comments on commit ffb15c2

Please sign in to comment.