From 833684b09561956b58dbc9fd261336ab135bcaf1 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sat, 4 Feb 2023 16:05:03 +1100 Subject: [PATCH] Replace dep `futures-util` with `futures-lite` in binstalk-downloader (#764) `futures-util` has too many dependencies and it contains a lot of code of which we only use a tiny bit of them. Signed-off-by: Jiahao XU --- Cargo.lock | 12 ++++- crates/binstalk-downloader/Cargo.toml | 2 +- crates/binstalk-downloader/src/download.rs | 2 +- .../src/download/async_extracter.rs | 4 +- .../src/download/async_tar_visitor.rs | 2 +- .../src/download/zip_extraction.rs | 53 +++++++++++-------- crates/binstalk-downloader/src/remote.rs | 2 +- 7 files changed, 47 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7251166c..1e38ed72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,7 +214,7 @@ dependencies = [ "compact_str", "digest", "flate2", - "futures-util", + "futures-lite", "generic-array", "httpdate", "reqwest", @@ -847,6 +847,16 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfb8371b6fb2aeb2d280374607aeabfc99d95c72edfe51692e42d3d7f0d08531" +[[package]] +name = "futures-lite" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48" +dependencies = [ + "futures-core", + "pin-project-lite", +] + [[package]] name = "futures-macro" version = "0.3.26" diff --git a/crates/binstalk-downloader/Cargo.toml b/crates/binstalk-downloader/Cargo.toml index c7ff4790..a9c01de0 100644 --- a/crates/binstalk-downloader/Cargo.toml +++ b/crates/binstalk-downloader/Cargo.toml @@ -19,7 +19,7 @@ bzip2 = "0.4.4" compact_str = "0.6.1" digest = "0.10.6" flate2 = { version = "1.0.25", default-features = false } -futures-util = { version = "0.3.26", default-features = false, features = ["std"] } +futures-lite = { version = "1.12.0", default-features = false } generic-array = "0.14.6" httpdate = "1.0.2" reqwest = { version = "0.11.14", features = ["stream", "gzip", "brotli", "deflate"], default-features = false } diff --git a/crates/binstalk-downloader/src/download.rs b/crates/binstalk-downloader/src/download.rs index ca3fe268..2d491239 100644 --- a/crates/binstalk-downloader/src/download.rs +++ b/crates/binstalk-downloader/src/download.rs @@ -2,7 +2,7 @@ use std::{fmt::Debug, io, marker::PhantomData, path::Path}; use binstalk_types::cargo_toml_binstall::PkgFmtDecomposed; use digest::{Digest, FixedOutput, HashMarker, Output, OutputSizeUser, Update}; -use futures_util::stream::StreamExt; +use futures_lite::stream::StreamExt; use thiserror::Error as ThisError; use tracing::{debug, instrument}; diff --git a/crates/binstalk-downloader/src/download/async_extracter.rs b/crates/binstalk-downloader/src/download/async_extracter.rs index 4850771e..ecb087a5 100644 --- a/crates/binstalk-downloader/src/download/async_extracter.rs +++ b/crates/binstalk-downloader/src/download/async_extracter.rs @@ -7,8 +7,8 @@ use std::{ use async_zip::read::stream::ZipFileReader; use bytes::{Bytes, BytesMut}; -use futures_util::{ - future::try_join, +use futures_lite::{ + future::try_zip as try_join, stream::{Stream, StreamExt}, }; use tokio::sync::mpsc; diff --git a/crates/binstalk-downloader/src/download/async_tar_visitor.rs b/crates/binstalk-downloader/src/download/async_tar_visitor.rs index 9c44506c..af1abba2 100644 --- a/crates/binstalk-downloader/src/download/async_tar_visitor.rs +++ b/crates/binstalk-downloader/src/download/async_tar_visitor.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, fmt::Debug, io, path::Path, pin::Pin}; use async_compression::tokio::bufread; use bytes::Bytes; -use futures_util::stream::{Stream, StreamExt}; +use futures_lite::stream::{Stream, StreamExt}; use tokio::io::{copy, sink, AsyncRead}; use tokio_tar::{Archive, Entry, EntryType}; use tokio_util::io::StreamReader; diff --git a/crates/binstalk-downloader/src/download/zip_extraction.rs b/crates/binstalk-downloader/src/download/zip_extraction.rs index 89e4f2ab..69cfcb17 100644 --- a/crates/binstalk-downloader/src/download/zip_extraction.rs +++ b/crates/binstalk-downloader/src/download/zip_extraction.rs @@ -5,7 +5,7 @@ use std::{ use async_zip::{read::ZipEntryReader, ZipEntryExt}; use bytes::{Bytes, BytesMut}; -use futures_util::future::{try_join, TryFutureExt}; +use futures_lite::future::try_zip as try_join; use thiserror::Error as ThisError; use tokio::{ io::{AsyncRead, AsyncReadExt}, @@ -78,29 +78,36 @@ where let (tx, mut rx) = mpsc::channel::(5); // This entry is a file. + + let write_task = asyncify(move || { + if let Some(p) = outpath.parent() { + std::fs::create_dir_all(p)?; + } + let mut outfile = std::fs::File::create(&outpath)?; + + while let Some(bytes) = rx.blocking_recv() { + outfile.write_all(&bytes)?; + } + + outfile.flush()?; + + if let Some(perms) = perms { + outfile.set_permissions(perms)?; + } + + Ok(()) + }); + + let read_task = copy_file_to_mpsc(entry, tx, buf); + try_join( - asyncify(move || { - if let Some(p) = outpath.parent() { - std::fs::create_dir_all(p)?; - } - let mut outfile = std::fs::File::create(&outpath)?; - - while let Some(bytes) = rx.blocking_recv() { - outfile.write_all(&bytes)?; - } - - outfile.flush()?; - - if let Some(perms) = perms { - outfile.set_permissions(perms)?; - } - - Ok(()) - }) - .err_into(), - copy_file_to_mpsc(entry, tx, buf) - .map_err(ZipError::from_inner) - .map_err(DownloadError::from), + async move { write_task.await.map_err(From::from) }, + async move { + read_task + .await + .map_err(ZipError::from_inner) + .map_err(DownloadError::from) + }, ) .await?; } diff --git a/crates/binstalk-downloader/src/remote.rs b/crates/binstalk-downloader/src/remote.rs index 53f69170..56eb42b3 100644 --- a/crates/binstalk-downloader/src/remote.rs +++ b/crates/binstalk-downloader/src/remote.rs @@ -5,7 +5,7 @@ use std::{ }; use bytes::Bytes; -use futures_util::stream::{Stream, StreamExt}; +use futures_lite::stream::{Stream, StreamExt}; use httpdate::parse_http_date; use reqwest::{ header::{HeaderMap, RETRY_AFTER},