diff --git a/examples/download_progress/src/download.rs b/examples/download_progress/src/download.rs index d63fb906df..5b81f7a2d4 100644 --- a/examples/download_progress/src/download.rs +++ b/examples/download_progress/src/download.rs @@ -1,16 +1,14 @@ -use iced::futures::{SinkExt, Stream, StreamExt}; -use iced::stream::try_channel; +use iced::futures::StreamExt; +use iced::task::{sipper, Straw}; use std::sync::Arc; -pub fn download( - url: impl AsRef, -) -> impl Stream> { - try_channel(1, move |mut output| async move { +pub fn download(url: impl AsRef) -> impl Straw<(), Progress, Error> { + sipper(move |mut progress| async move { let response = reqwest::get(url.as_ref()).await?; let total = response.content_length().ok_or(Error::NoContentLength)?; - let _ = output.send(Progress::Downloading { percent: 0.0 }).await; + let _ = progress.send(Progress { percent: 0.0 }).await; let mut byte_stream = response.bytes_stream(); let mut downloaded = 0; @@ -19,23 +17,20 @@ pub fn download( let bytes = next_bytes?; downloaded += bytes.len(); - let _ = output - .send(Progress::Downloading { + let _ = progress + .send(Progress { percent: 100.0 * downloaded as f32 / total as f32, }) .await; } - let _ = output.send(Progress::Finished).await; - Ok(()) }) } #[derive(Debug, Clone)] -pub enum Progress { - Downloading { percent: f32 }, - Finished, +pub struct Progress { + pub percent: f32, } #[derive(Debug, Clone)] diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index f4b0720338..8f59f1dc28 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -25,7 +25,7 @@ struct Example { pub enum Message { Add, Download(usize), - DownloadProgressed(usize, Result), + DownloadUpdated(usize, Update), } impl Example { @@ -52,15 +52,13 @@ impl Example { let task = download.start(); - task.map(move |progress| { - Message::DownloadProgressed(index, progress) - }) + task.map(move |update| Message::DownloadUpdated(index, update)) } - Message::DownloadProgressed(id, progress) => { + Message::DownloadUpdated(id, update) => { if let Some(download) = self.downloads.iter_mut().find(|download| download.id == id) { - download.progress(progress); + download.update(update); } Task::none() @@ -95,6 +93,12 @@ struct Download { state: State, } +#[derive(Debug, Clone)] +pub enum Update { + Downloading(download::Progress), + Finished(Result<(), download::Error>), +} + #[derive(Debug)] enum State { Idle, @@ -111,18 +115,20 @@ impl Download { } } - pub fn start( - &mut self, - ) -> Task> { + pub fn start(&mut self) -> Task { match self.state { State::Idle { .. } | State::Finished { .. } | State::Errored { .. } => { - let (task, handle) = Task::stream(download( - "https://huggingface.co/\ + let (task, handle) = Task::sip( + download( + "https://huggingface.co/\ mattshumer/Reflection-Llama-3.1-70B/\ resolve/main/model-00001-of-00162.safetensors", - )) + ), + Update::Downloading, + Update::Finished, + ) .abortable(); self.state = State::Downloading { @@ -136,20 +142,18 @@ impl Download { } } - pub fn progress( - &mut self, - new_progress: Result, - ) { + pub fn update(&mut self, update: Update) { if let State::Downloading { progress, .. } = &mut self.state { - match new_progress { - Ok(download::Progress::Downloading { percent }) => { - *progress = percent; - } - Ok(download::Progress::Finished) => { - self.state = State::Finished; + match update { + Update::Downloading(new_progress) => { + *progress = new_progress.percent; } - Err(_error) => { - self.state = State::Errored; + Update::Finished(result) => { + self.state = if result.is_ok() { + State::Finished + } else { + State::Errored + }; } } }