Skip to content

Commit

Permalink
Change to url::Url instead of plain String
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Apr 27, 2020
1 parent bb26fc5 commit 0c2740f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion examples/download_progress/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ iced_native = { path = "../../native" }
iced_futures = { path = "../../futures" }
reqwest = "0.10"
futures-util = "0.3.4"
bytes = "0.5.4"
bytes = "0.5.4"
url = "2.1.1"
13 changes: 7 additions & 6 deletions examples/download_progress/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
Expand All @@ -24,7 +25,7 @@ impl<H, I> iced_native::subscription::Recipe<H, I> 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::<Self>().hash(state);
Expand All @@ -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((
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions examples/download_progress/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use url::Url;

use iced::{
button, scrollable, Align, Application, Button, Column, Command, Container,
Element, HorizontalAlignment, Length, ProgressBar, Row, Scrollable,
Expand Down Expand Up @@ -223,10 +225,13 @@ impl Application for Example {
// Just a little utility function
fn file<T: ToString>(url: T) -> iced::Subscription<Message> {
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),
)
})
}

Expand Down

0 comments on commit 0c2740f

Please sign in to comment.