mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 14:28:42 +00:00
Optimize Download::and_visit_tar
: Use trait object to avoid monomorphization (#644)
by removing method `TarEntriesVisitor::finish` and associated type `TarEntriesVisitor::Target`. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
1ab979cde8
commit
959b465d81
4 changed files with 30 additions and 37 deletions
|
@ -101,11 +101,11 @@ impl Download {
|
|||
/// NOTE that this API does not support gnu extension sparse file unlike
|
||||
/// [`Download::and_extract`].
|
||||
#[instrument(skip(visitor))]
|
||||
pub async fn and_visit_tar<V: TarEntriesVisitor + Debug + Send + 'static>(
|
||||
pub async fn and_visit_tar(
|
||||
self,
|
||||
fmt: TarBasedFmt,
|
||||
visitor: V,
|
||||
) -> Result<V::Target, DownloadError> {
|
||||
visitor: &mut dyn TarEntriesVisitor,
|
||||
) -> Result<(), DownloadError> {
|
||||
let stream = self
|
||||
.client
|
||||
.get_stream(self.url)
|
||||
|
@ -114,11 +114,11 @@ impl Download {
|
|||
|
||||
debug!("Downloading and extracting then in-memory processing");
|
||||
|
||||
let ret = extract_tar_based_stream_and_visit(stream, fmt, visitor).await?;
|
||||
extract_tar_based_stream_and_visit(stream, fmt, visitor).await?;
|
||||
|
||||
debug!("Download, extraction and in-memory procession OK");
|
||||
|
||||
Ok(ret)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Download a file from the provided URL and extract it to the provided path.
|
||||
|
|
|
@ -84,21 +84,17 @@ pub enum TarEntryType {
|
|||
/// Entires can be in arbitary order.
|
||||
#[async_trait::async_trait]
|
||||
pub trait TarEntriesVisitor: Send + Sync {
|
||||
type Target;
|
||||
|
||||
/// Will be called once per entry
|
||||
async fn visit(&mut self, entry: &mut dyn TarEntry) -> Result<(), DownloadError>;
|
||||
fn finish(self) -> Result<Self::Target, DownloadError>;
|
||||
}
|
||||
|
||||
pub(crate) async fn extract_tar_based_stream_and_visit<S, V>(
|
||||
pub(crate) async fn extract_tar_based_stream_and_visit<S>(
|
||||
stream: S,
|
||||
fmt: TarBasedFmt,
|
||||
mut visitor: V,
|
||||
) -> Result<V::Target, DownloadError>
|
||||
visitor: &mut dyn TarEntriesVisitor,
|
||||
) -> Result<(), DownloadError>
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, DownloadError>> + Send + Sync,
|
||||
V: TarEntriesVisitor,
|
||||
{
|
||||
debug!("Extracting from {fmt} archive to process it in memory");
|
||||
|
||||
|
@ -125,5 +121,5 @@ where
|
|||
copy(&mut entry, &mut sink).await?;
|
||||
}
|
||||
|
||||
visitor.finish()
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue