Optimize binary size/compilation time

by reducing generics monomorphization using `Box<dyn Trait>`.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-12 19:44:59 +10:00
parent bd39ce754f
commit 3a1038c80b
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -109,14 +109,9 @@ impl<T: Debug + Send + 'static> AsyncExtracterInner<T> {
}
}
async fn extract_impl<
F: FnOnce(mpsc::Receiver<Content>) -> Result<T, BinstallError> + Send + 'static,
T: Debug + Send + 'static,
S: Stream<Item = Result<Bytes, E>> + Unpin,
E,
>(
async fn extract_impl<T: Debug + Send + 'static, S: Stream<Item = Result<Bytes, E>> + Unpin, E>(
mut stream: S,
f: F,
f: Box<dyn FnOnce(mpsc::Receiver<Content>) -> Result<T, BinstallError> + Send>,
) -> Result<T, BinstallError>
where
BinstallError: From<E>,
@ -155,7 +150,9 @@ where
{
let path = output.to_owned();
extract_impl(stream, move |mut rx| {
extract_impl(
stream,
Box::new(move |mut rx| {
fs::create_dir_all(path.parent().unwrap())?;
let mut file = fs::File::create(&path)?;
@ -173,7 +170,8 @@ where
ScopeGuard::into_inner(remove_guard);
Ok(())
})
}),
)
.await
}
@ -186,7 +184,9 @@ where
{
let path = output.to_owned();
extract_impl(stream, move |mut rx| {
extract_impl(
stream,
Box::new(move |mut rx| {
fs::create_dir_all(path.parent().unwrap())?;
let mut file = tempfile()?;
@ -197,7 +197,8 @@ where
file.rewind()?;
unzip(file, &path)
})
}),
)
.await
}
@ -219,7 +220,9 @@ where
let path = output.to_owned();
extract_impl(stream, move |mut rx| {
extract_impl(
stream,
Box::new(move |mut rx| {
fs::create_dir_all(path.parent().unwrap())?;
extract_compressed_from_readable::<DummyVisitor, _>(
@ -227,7 +230,8 @@ where
fmt,
Op::UnpackToPath(&path),
)
})
}),
)
.await
}
@ -239,9 +243,12 @@ pub async fn extract_tar_based_stream_and_visit<V: TarEntriesVisitor + Debug + S
where
BinstallError: From<E>,
{
extract_impl(stream, move |mut rx| {
extract_impl(
stream,
Box::new(move |mut rx| {
extract_compressed_from_readable(ReadableRx::new(&mut rx), fmt, Op::Visit(&mut visitor))
.map(|_| visitor)
})
}),
)
.await
}