From 784a24577b349af01ac82717d253a620d1cddb8b Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sat, 18 Jun 2022 18:37:50 +1000 Subject: [PATCH] Refactor: Rm `extract_impl` Signed-off-by: Jiahao XU --- src/helpers/async_extracter.rs | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/helpers/async_extracter.rs b/src/helpers/async_extracter.rs index 2a2a42dd..5a7ef9c1 100644 --- a/src/helpers/async_extracter.rs +++ b/src/helpers/async_extracter.rs @@ -28,22 +28,13 @@ use tokio::task::block_in_place; use super::{extracter::*, stream_readable::StreamReadable}; use crate::{BinstallError, TarBasedFmt}; -async fn extract_impl(stream: S, f: F) -> Result -where - S: Stream> + Unpin, - F: FnOnce(StreamReadable) -> Result, - BinstallError: From, -{ - let readable = StreamReadable::new(stream).await; - block_in_place(move || f(readable)) -} - pub async fn extract_bin(stream: S, path: &Path) -> Result<(), BinstallError> where S: Stream> + Unpin + 'static, BinstallError: From, { - extract_impl(stream, move |mut reader| { + let mut reader = StreamReadable::new(stream).await; + block_in_place(move || { fs::create_dir_all(path.parent().unwrap())?; let mut file = fs::File::create(&path)?; @@ -62,7 +53,6 @@ where Ok(()) }) - .await } pub async fn extract_zip(stream: S, path: &Path) -> Result<(), BinstallError> @@ -70,7 +60,8 @@ where S: Stream> + Unpin + 'static, BinstallError: From, { - extract_impl(stream, move |mut reader| { + let mut reader = StreamReadable::new(stream).await; + block_in_place(move || { fs::create_dir_all(path.parent().unwrap())?; let mut file = tempfile()?; @@ -82,7 +73,6 @@ where unzip(file, path) }) - .await } pub async fn extract_tar_based_stream( @@ -94,7 +84,8 @@ where S: Stream> + Unpin + 'static, BinstallError: From, { - extract_impl(stream, move |reader| { + let reader = StreamReadable::new(stream).await; + block_in_place(move || { fs::create_dir_all(path.parent().unwrap())?; debug!("Extracting from {fmt} archive to {path:#?}"); @@ -103,7 +94,6 @@ where Ok(()) }) - .await } /// Visitor must iterate over all entries. @@ -128,12 +118,12 @@ where V: TarEntriesVisitor + Debug + Send + 'static, BinstallError: From, { - extract_impl(stream, move |reader| { + let reader = StreamReadable::new(stream).await; + block_in_place(move || { debug!("Extracting from {fmt} archive to process it in memory"); let mut tar = create_tar_decoder(reader, fmt)?; visitor.visit(tar.entries()?)?; Ok(visitor) }) - .await }