Skip to content

Commit 05618ea

Browse files
committed
Use sipper in download_progress
1 parent 54ffbbf commit 05618ea

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

examples/download_progress/src/download.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use iced::futures::{SinkExt, Stream, StreamExt};
2-
use iced::stream::try_channel;
1+
use iced::futures::StreamExt;
2+
use iced::task::{sipper, Straw};
33

44
use std::sync::Arc;
55

6-
pub fn download(
7-
url: impl AsRef<str>,
8-
) -> impl Stream<Item = Result<Progress, Error>> {
9-
try_channel(1, move |mut output| async move {
6+
pub fn download(url: impl AsRef<str>) -> impl Straw<(), Progress, Error> {
7+
sipper(move |mut progress| async move {
108
let response = reqwest::get(url.as_ref()).await?;
119
let total = response.content_length().ok_or(Error::NoContentLength)?;
1210

13-
let _ = output.send(Progress::Downloading { percent: 0.0 }).await;
11+
let _ = progress.send(Progress { percent: 0.0 }).await;
1412

1513
let mut byte_stream = response.bytes_stream();
1614
let mut downloaded = 0;
@@ -19,23 +17,20 @@ pub fn download(
1917
let bytes = next_bytes?;
2018
downloaded += bytes.len();
2119

22-
let _ = output
23-
.send(Progress::Downloading {
20+
let _ = progress
21+
.send(Progress {
2422
percent: 100.0 * downloaded as f32 / total as f32,
2523
})
2624
.await;
2725
}
2826

29-
let _ = output.send(Progress::Finished).await;
30-
3127
Ok(())
3228
})
3329
}
3430

3531
#[derive(Debug, Clone)]
36-
pub enum Progress {
37-
Downloading { percent: f32 },
38-
Finished,
32+
pub struct Progress {
33+
pub percent: f32,
3934
}
4035

4136
#[derive(Debug, Clone)]

examples/download_progress/src/main.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct Example {
2525
pub enum Message {
2626
Add,
2727
Download(usize),
28-
DownloadProgressed(usize, Result<download::Progress, download::Error>),
28+
DownloadUpdated(usize, Update),
2929
}
3030

3131
impl Example {
@@ -52,15 +52,13 @@ impl Example {
5252

5353
let task = download.start();
5454

55-
task.map(move |progress| {
56-
Message::DownloadProgressed(index, progress)
57-
})
55+
task.map(move |update| Message::DownloadUpdated(index, update))
5856
}
59-
Message::DownloadProgressed(id, progress) => {
57+
Message::DownloadUpdated(id, update) => {
6058
if let Some(download) =
6159
self.downloads.iter_mut().find(|download| download.id == id)
6260
{
63-
download.progress(progress);
61+
download.update(update);
6462
}
6563

6664
Task::none()
@@ -95,6 +93,12 @@ struct Download {
9593
state: State,
9694
}
9795

96+
#[derive(Debug, Clone)]
97+
pub enum Update {
98+
Downloading(download::Progress),
99+
Finished(Result<(), download::Error>),
100+
}
101+
98102
#[derive(Debug)]
99103
enum State {
100104
Idle,
@@ -111,18 +115,20 @@ impl Download {
111115
}
112116
}
113117

114-
pub fn start(
115-
&mut self,
116-
) -> Task<Result<download::Progress, download::Error>> {
118+
pub fn start(&mut self) -> Task<Update> {
117119
match self.state {
118120
State::Idle { .. }
119121
| State::Finished { .. }
120122
| State::Errored { .. } => {
121-
let (task, handle) = Task::stream(download(
122-
"https://huggingface.co/\
123+
let (task, handle) = Task::sip(
124+
download(
125+
"https://huggingface.co/\
123126
mattshumer/Reflection-Llama-3.1-70B/\
124127
resolve/main/model-00001-of-00162.safetensors",
125-
))
128+
),
129+
Update::Downloading,
130+
Update::Finished,
131+
)
126132
.abortable();
127133

128134
self.state = State::Downloading {
@@ -136,20 +142,18 @@ impl Download {
136142
}
137143
}
138144

139-
pub fn progress(
140-
&mut self,
141-
new_progress: Result<download::Progress, download::Error>,
142-
) {
145+
pub fn update(&mut self, update: Update) {
143146
if let State::Downloading { progress, .. } = &mut self.state {
144-
match new_progress {
145-
Ok(download::Progress::Downloading { percent }) => {
146-
*progress = percent;
147-
}
148-
Ok(download::Progress::Finished) => {
149-
self.state = State::Finished;
147+
match update {
148+
Update::Downloading(new_progress) => {
149+
*progress = new_progress.percent;
150150
}
151-
Err(_error) => {
152-
self.state = State::Errored;
151+
Update::Finished(result) => {
152+
self.state = if result.is_ok() {
153+
State::Finished
154+
} else {
155+
State::Errored
156+
};
153157
}
154158
}
155159
}

0 commit comments

Comments
 (0)