diff --git a/examples/download_progress/Cargo.toml b/examples/download_progress/Cargo.toml index 2ce0f1435b..6094cceb65 100644 --- a/examples/download_progress/Cargo.toml +++ b/examples/download_progress/Cargo.toml @@ -11,4 +11,5 @@ iced_native = { path = "../../native" } iced_futures = { path = "../../futures" } reqwest = "0.10" futures-util = "0.3.4" -bytes = "0.5.4" \ No newline at end of file +bytes = "0.5.4" +url = "2.1.1" \ No newline at end of file diff --git a/examples/download_progress/src/download.rs b/examples/download_progress/src/download.rs index a805878ca6..0a1f808546 100644 --- a/examples/download_progress/src/download.rs +++ b/examples/download_progress/src/download.rs @@ -3,15 +3,16 @@ use std::hash::Hash; use bytes::BufMut; use futures_util::stream::{self, BoxStream}; use reqwest; +use url::Url; pub struct Download { - pub url: String, + pub url: Url, } pub enum State { - Ready(String), + Ready(Url), Downloading { - url: String, + url: Url, response: reqwest::Response, total: u64, bytes: Vec, @@ -24,7 +25,7 @@ impl iced_native::subscription::Recipe for Download where H: std::hash::Hasher, { - type Output = (String, Progress); + type Output = (Url, Progress); fn hash(&self, state: &mut H) { std::any::TypeId::of::().hash(state); @@ -37,7 +38,7 @@ where ) -> BoxStream<'static, Self::Output> { Box::pin(stream::unfold(State::Ready(self.url), |state| async move { match state { - State::Ready(url) => match reqwest::get(&url).await { + State::Ready(url) => match reqwest::get(url.as_str()).await { Ok(response) => { if let Some(total) = response.content_length() { Some(( @@ -68,7 +69,7 @@ where (downloaded as f32 / total as f32) * 100.0; bytes.put(chunk); Some(( - (url.to_string(), Progress::Advanced(percentage)), + (url.clone(), Progress::Advanced(percentage)), State::Downloading { url, response, diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index 6be0a5c68c..bd4b4684d5 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -1,3 +1,5 @@ +use url::Url; + use iced::{ button, scrollable, Align, Application, Button, Column, Command, Container, Element, HorizontalAlignment, Length, ProgressBar, Row, Scrollable, @@ -223,10 +225,13 @@ impl Application for Example { // Just a little utility function fn file(url: T) -> iced::Subscription { iced::Subscription::from_recipe(Download { - url: url.to_string(), + url: Url::parse(&url.to_string()).unwrap(), }) .map(|(url, progress)| { - Message::from(url, DownloadMessage::DownloadProgressed(progress)) + Message::from( + url.as_str().to_string(), + DownloadMessage::DownloadProgressed(progress), + ) }) }