mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 13:08:42 +00:00
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:
parent
bd39ce754f
commit
3a1038c80b
1 changed files with 48 additions and 41 deletions
|
@ -109,14 +109,9 @@ impl<T: Debug + Send + 'static> AsyncExtracterInner<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn extract_impl<
|
async fn extract_impl<T: Debug + Send + 'static, S: Stream<Item = Result<Bytes, E>> + Unpin, E>(
|
||||||
F: FnOnce(mpsc::Receiver<Content>) -> Result<T, BinstallError> + Send + 'static,
|
|
||||||
T: Debug + Send + 'static,
|
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin,
|
|
||||||
E,
|
|
||||||
>(
|
|
||||||
mut stream: S,
|
mut stream: S,
|
||||||
f: F,
|
f: Box<dyn FnOnce(mpsc::Receiver<Content>) -> Result<T, BinstallError> + Send>,
|
||||||
) -> Result<T, BinstallError>
|
) -> Result<T, BinstallError>
|
||||||
where
|
where
|
||||||
BinstallError: From<E>,
|
BinstallError: From<E>,
|
||||||
|
@ -155,25 +150,28 @@ where
|
||||||
{
|
{
|
||||||
let path = output.to_owned();
|
let path = output.to_owned();
|
||||||
|
|
||||||
extract_impl(stream, move |mut rx| {
|
extract_impl(
|
||||||
fs::create_dir_all(path.parent().unwrap())?;
|
stream,
|
||||||
|
Box::new(move |mut rx| {
|
||||||
|
fs::create_dir_all(path.parent().unwrap())?;
|
||||||
|
|
||||||
let mut file = fs::File::create(&path)?;
|
let mut file = fs::File::create(&path)?;
|
||||||
|
|
||||||
// remove it unless the operation isn't aborted and no write
|
// remove it unless the operation isn't aborted and no write
|
||||||
// fails.
|
// fails.
|
||||||
let remove_guard = guard(&path, |path| {
|
let remove_guard = guard(&path, |path| {
|
||||||
fs::remove_file(path).ok();
|
fs::remove_file(path).ok();
|
||||||
});
|
});
|
||||||
|
|
||||||
read_into_file(&mut file, &mut rx)?;
|
read_into_file(&mut file, &mut rx)?;
|
||||||
|
|
||||||
// Operation isn't aborted and all writes succeed,
|
// Operation isn't aborted and all writes succeed,
|
||||||
// disarm the remove_guard.
|
// disarm the remove_guard.
|
||||||
ScopeGuard::into_inner(remove_guard);
|
ScopeGuard::into_inner(remove_guard);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,18 +184,21 @@ where
|
||||||
{
|
{
|
||||||
let path = output.to_owned();
|
let path = output.to_owned();
|
||||||
|
|
||||||
extract_impl(stream, move |mut rx| {
|
extract_impl(
|
||||||
fs::create_dir_all(path.parent().unwrap())?;
|
stream,
|
||||||
|
Box::new(move |mut rx| {
|
||||||
|
fs::create_dir_all(path.parent().unwrap())?;
|
||||||
|
|
||||||
let mut file = tempfile()?;
|
let mut file = tempfile()?;
|
||||||
|
|
||||||
read_into_file(&mut file, &mut rx)?;
|
read_into_file(&mut file, &mut rx)?;
|
||||||
|
|
||||||
// rewind it so that we can pass it to unzip
|
// rewind it so that we can pass it to unzip
|
||||||
file.rewind()?;
|
file.rewind()?;
|
||||||
|
|
||||||
unzip(file, &path)
|
unzip(file, &path)
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,15 +220,18 @@ where
|
||||||
|
|
||||||
let path = output.to_owned();
|
let path = output.to_owned();
|
||||||
|
|
||||||
extract_impl(stream, move |mut rx| {
|
extract_impl(
|
||||||
fs::create_dir_all(path.parent().unwrap())?;
|
stream,
|
||||||
|
Box::new(move |mut rx| {
|
||||||
|
fs::create_dir_all(path.parent().unwrap())?;
|
||||||
|
|
||||||
extract_compressed_from_readable::<DummyVisitor, _>(
|
extract_compressed_from_readable::<DummyVisitor, _>(
|
||||||
ReadableRx::new(&mut rx),
|
ReadableRx::new(&mut rx),
|
||||||
fmt,
|
fmt,
|
||||||
Op::UnpackToPath(&path),
|
Op::UnpackToPath(&path),
|
||||||
)
|
)
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,9 +243,12 @@ pub async fn extract_tar_based_stream_and_visit<V: TarEntriesVisitor + Debug + S
|
||||||
where
|
where
|
||||||
BinstallError: From<E>,
|
BinstallError: From<E>,
|
||||||
{
|
{
|
||||||
extract_impl(stream, move |mut rx| {
|
extract_impl(
|
||||||
extract_compressed_from_readable(ReadableRx::new(&mut rx), fmt, Op::Visit(&mut visitor))
|
stream,
|
||||||
.map(|_| visitor)
|
Box::new(move |mut rx| {
|
||||||
})
|
extract_compressed_from_readable(ReadableRx::new(&mut rx), fmt, Op::Visit(&mut visitor))
|
||||||
|
.map(|_| visitor)
|
||||||
|
}),
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue